Skip to content

Commit

Permalink
Merge pull request maxdemarzi#74 from blizkreeg/models
Browse files Browse the repository at this point in the history
added reverse casting (used to store date/time) and Date/Time data type ...
  • Loading branch information
blizkreeg committed Dec 13, 2013
2 parents badeecd + d3ef62a commit 67bf721
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ GEM
slop (3.4.6)
thread_safe (0.1.3)
atomic
timecop (0.7.0)
tzinfo (0.3.38)

PLATFORMS
Expand All @@ -79,3 +80,4 @@ DEPENDENCIES
neography (~> 1.1.3)
pry
rspec (~> 2.13.0)
timecop
1 change: 1 addition & 0 deletions deja.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "pry"
s.add_development_dependency "rspec", "~>2.13.0"
s.add_development_dependency 'ci_reporter'
s.add_development_dependency 'timecop'

s.add_dependency "activemodel", "~> 4.0.0"
s.add_dependency "activesupport", "~> 4.0.0"
Expand Down
3 changes: 3 additions & 0 deletions lib/deja/schema_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def attribute(name, type, opts = {})
def attr_accessorize(name, opts)
send(:attr_accessor, name)
define_attribute_methods name
define_method("#{name}") do
reversecast(name, instance_variable_get("@#{name}"))
end
define_method("#{name}=") do |new_value|
casted_value = typecast(name, new_value)
send("#{name}_will_change!") if (casted_value != instance_variable_get("@#{name}") && !instance_variable_get("@#{name}").nil?)
Expand Down
21 changes: 21 additions & 0 deletions lib/deja/type_caster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ module TypeCaster
extend ActiveSupport::Concern

included do
# cast back to Ruby objects where representation in the graph is different
def reversecast(attr_name, value)
return nil if value.nil?

data_type = self.class.schema[:attributes][attr_name][:type].to_s

case data_type
when 'Date'
Date.parse(value.to_s)
when 'Time'
Time.at(value)
else
value
end
end

# cast to neo4j basic types and raise errors when invalid/unrecognized data type
def typecast(attr_name, value)
return nil if value.nil?

Expand All @@ -18,6 +35,10 @@ def typecast(attr_name, value)
Boolean.true?(value)
when 'String'
String(value)
when 'Date'
Date.parse(value.to_s).strftime("%Y%m%d").to_i
when 'Time'
value.to_i
else
raise TypeError, "undefined data type #{data_type} for attribute #{name}"
end
Expand Down
2 changes: 2 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Person < Deja::Node
attribute :bank_balance, Float
attribute :vip, Boolean
attribute :tags, Array
attribute :born_on, Date
attribute :knighted_at, Time

relationship :invested_in, :out => :investment
relationship :friends_with, :out => :friends
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'rspec'
require 'timecop'

# Load Deja lib
CURRENT_DIR = File.dirname(__FILE__)
Expand Down
16 changes: 16 additions & 0 deletions spec/type_caster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,21 @@
it 'should fail on unrecognized data type' do
expect { person.tags = ['engineer', 'designer', 'entrepreneur'] }.to raise_error TypeError
end

it 'should validate date values' do
Timecop.freeze do
person.born_on = Date.today
expect(person.instance_variable_get("@born_on")).to eq(Date.today.strftime("%Y%m%d").to_i)
expect(person.born_on).to eq(Date.today)
end
end

it 'should validate time values' do
Timecop.freeze do
person.knighted_at = Time.now
expect(person.instance_variable_get("@knighted_at")).to eq(Time.now.to_i)
expect(person.knighted_at.to_i).to eq(Time.now.to_i)
end
end
end
end

0 comments on commit 67bf721

Please sign in to comment.