Skip to content

Commit

Permalink
refactoring, DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Jan 26, 2015
1 parent 59f2537 commit 2556530
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
6 changes: 2 additions & 4 deletions lib/neo4j/shared/property.rb
Expand Up @@ -53,9 +53,7 @@ def default_properties
end

def send_props(hash)
hash.each do |key, value|
self.send("#{key}=", value)
end
hash.each { |key, value| self.send("#{key}=", value) }
end

private
Expand Down Expand Up @@ -228,7 +226,7 @@ def type_converter(options)
return unless converter
options[:type] = converter.convert_type
options[:typecaster] = ActiveAttr::Typecasting::ObjectTypecaster.new
Neo4j::Shared::TypeConverters.add_converter(converter)
Neo4j::Shared::TypeConverters.register_converter(converter)
end

def magic_typecast(name, options)
Expand Down
50 changes: 17 additions & 33 deletions lib/neo4j/shared/type_converters.rb
Expand Up @@ -112,15 +112,14 @@ def convert_properties_to(medium, properties)

properties.each_with_object({}) do |(attr, value), new_attributes|
next new_attributes if skip_conversion?(attr, value)
primitive = primitive_type(attr.to_sym)
new_attributes[attr] = converted_property(primitive, value, converter)
new_attributes[attr] = converted_property(primitive_type(attr.to_sym), value, converter)
end
end

private

def converted_property(type, value, converter)
TypeConverters.converters[type].nil? ? value : TypeConverters.send(converter, value, type)
TypeConverters.converters[type].nil? ? value : TypeConverters.to_other(converter, value, type)
end

# If the attribute is to be typecast using a custom converter, which converter should it use? If no, returns the type to find a native serializer.
Expand All @@ -135,52 +134,37 @@ def primitive_type(attr)
end
end

# Returns true if the property isn't defined in the model or it's both nil and unchanged.
def skip_conversion?(attr, value)
!self.class.attributes[attr] || (value.nil? && !changed_attributes.key?(attr))
end

def self.included(_base)
init
end

class << self
def typecaster_for(primitive_type)
converters.key?(primitive_type) ? converters[primitive_type] : nil
def included(_)
return if @converters
@converters = {}
Neo4j::Shared::TypeConverters.constants.each do |constant_name|
constant = Neo4j::Shared::TypeConverters.const_get(constant_name)
register_converter(constant) if constant.respond_to?(:convert_type)
end
end

# Converts the value to ruby from a Neo4j database value if there is a converter for given type
def to_ruby(value, type = nil)
found_converter = converters[type]
found_converter ? found_converter.to_ruby(value) : value
def typecaster_for(primitive_type)
converters.key?(primitive_type) ? converters[primitive_type] : nil
end

# Converts the value to a Neo4j database value from ruby if there is a converter for given type
def to_db(value, type = nil)
# @param [Symbol] direction either :to_ruby or :to_other
def to_other(direction, value, type)
fail "Unknown direction given: #{direction}" unless direction == :to_ruby || direction == :to_db
found_converter = converters[type]
found_converter ? found_converter.to_db(value) : value
end

def add_converter(converter)
@converters ||= {}
@converters[converter.convert_type] = converter
end

def init
return if @converters
Neo4j::Shared::TypeConverters.constants.each do |constant_name|
constant = Neo4j::Shared::TypeConverters.const_get(constant_name)
add_converter(constant) if constant.respond_to?(:convert_type)
end
found_converter ? found_converter.send(direction, value) : value
end

def register_converter(converter)
converters[converter.convert_type] = converter
end

def converters
init
@converters
end
attr_reader :converters
end
end
end
8 changes: 4 additions & 4 deletions spec/integration/type_converters/type_converters_spec.rb
Expand Up @@ -26,7 +26,7 @@
describe 'to_ruby' do
it 'converts if there is a converter' do
date_time = Time.utc(2011, 3, 2, 10, 0, 0).to_i
converter_value = Neo4j::Shared::TypeConverters.to_ruby(date_time, DateTime)
converter_value = Neo4j::Shared::TypeConverters.to_other(:to_ruby, date_time, DateTime)
converter_value.should be_a(DateTime)
converter_value.year.should eq(2011)
converter_value.month.should eq(3)
Expand All @@ -35,19 +35,19 @@
end

it 'returns the same value if there is no converter' do
Neo4j::Shared::TypeConverters.to_ruby(42, Integer).should eq(42)
Neo4j::Shared::TypeConverters.to_other(:to_ruby, 42, Integer).should eq(42)
end
end

describe 'to_db' do
it 'converts if there is a converter' do
date_time = DateTime.civil(2011, 3, 4, 1, 2, 3, 0)
converter_value = Neo4j::Shared::TypeConverters.to_db(date_time, DateTime)
converter_value = Neo4j::Shared::TypeConverters.to_other(:to_db, date_time, DateTime)
converter_value.should be_a(Integer)
end

it 'returns the same value if there is no converter' do
Neo4j::Shared::TypeConverters.to_ruby(42, Integer).should eq(42)
Neo4j::Shared::TypeConverters.to_other(:to_ruby, 42, Integer).should eq(42)
end
end

Expand Down

0 comments on commit 2556530

Please sign in to comment.