Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature for field method, passing a type option will accepts any active record sql types and type casts the value when it is set. #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_2.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_3.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_3.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ gem 'rspec', '~> 2.2.0'
gem 'wwtd'
gem 'rake'
gem 'json'
gem 'virtus'

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>= 1.3.6'
Expand Down
1 change: 1 addition & 0 deletions lib/active_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
require 'active_json/base'
require 'associations/associations'
require 'enum/enum'
require 'virtus'
19 changes: 17 additions & 2 deletions lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def field_names
@field_names ||= []
end

def types
@types ||= {}
end

def set_type(field_name, type)
types[field_name] = Virtus::Attribute.build(type)
end

def the_meta_class
class << self
self
Expand Down Expand Up @@ -199,6 +207,7 @@ def field(field_name, options = {})
validate_field(field_name)
field_names << field_name

set_type(field_name, options[:type])
define_getter_method(field_name, options[:default])
define_setter_method(field_name)
define_interrogator_method(field_name)
Expand Down Expand Up @@ -269,7 +278,7 @@ def define_setter_method(field)
method_name = "#{field}="
unless has_instance_method?(method_name)
define_method(method_name) do |new_val|
attributes[field] = new_val
attributes[field] = coerce(field, new_val)
end
end
end
Expand Down Expand Up @@ -374,7 +383,7 @@ def has_singleton_method?(name)

end

attr_reader :attributes
attr_reader :attributes, :types

def initialize(attributes = {})
attributes.symbolize_keys!
Expand Down Expand Up @@ -470,5 +479,11 @@ def marked_for_destruction?
false
end

def coerce(field, new_val)
type = self.class.types[field]
return attributes[field] = type.coerce(new_val) unless type.nil?
return new_value
end

end
end
35 changes: 35 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,41 @@ class Region < ActiveHash::Base
country.name.should == "foobar"
end
end

context "for fields with type values" do

before do
Country.field :population, :type => 'Integer', :default => false
end

context "entered values will be converted to set type" do

it 'when pass to new' do
country = Country.new(:population => "500000")
country.population.should == 500000
end

it 'when pass to setter' do
country = Country.new
country.population = '500000'
country.population.should == 500000
end

end

context 'valid types, accepts Ruby types' do

before do
Country.field :independence_day, :type => 'Date', :default => false
end

it 'date' do
country = Country.new(:independence_day => "01/12/1934")
country.independence_day.class.should == Date
end
end

end
end

describe "interrogator methods" do
Expand Down