Skip to content

Commit

Permalink
make serialize more similar to Rails, add YAML option
Browse files Browse the repository at this point in the history
  • Loading branch information
subvertallchris committed Jul 14, 2014
1 parent 2728ae6 commit 0de9f0e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
30 changes: 13 additions & 17 deletions lib/neo4j/active_node/serialized_properties.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
module Neo4j::ActiveNode
module SerializedProperties
module SerializedProperties
extend ActiveSupport::Concern

def serialized_properties
self.class.serialized_properties
self.class.serialized_properties
end

module ClassMethods
module ClassMethods

def serialized_properties=(name)
@serialize ||= []
@serialize.push name
end
def serialized_properties
@serialize || {}
end

def serialized_properties
@serialize || []
end

def serialize(name)
self.serialized_properties = name
end
end
end
end
def serialize(name, coder = JSON)
@serialize ||= {}
@serialize[name] = coder
end
end
end
end
29 changes: 26 additions & 3 deletions lib/neo4j/type_converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,34 @@ def to_ruby(value)
end
end

# Converts hash to/from JSON
class HashConverter
# Converts hash to/from YAML
class YAMLConverter
class << self

def convert_type
Hash
end

def to_db(value)
return nil if value.nil?
Psych.dump(value)
end

def to_ruby(value)
return nil if value.nil?
Psych.load(value)
end
end
end

# Converts hash to/from JSON
class JSONConverter
class << self

def convert_type
JSON
end

def to_db(value)
return nil if value.nil?
value.to_json
Expand All @@ -98,11 +118,14 @@ def to_ruby(value)
end
end



def convert_properties_to(medium, properties)
# Perform type conversion
serialize = self.respond_to?(:serialized_properties) ? self.serialized_properties : {}
properties = properties.inject({}) do |new_attributes, key_value_pair|
attr, value = key_value_pair
type = self.respond_to?(:serialized_properties) && self.serialized_properties.include?(attr.to_sym) ? Hash : self.class._attribute_type(attr)
type = serialize.has_key?(attr.to_sym) ? serialize[attr.to_sym] : self.class._attribute_type(attr)
new_attributes[attr] = if TypeConverters.converters[type].nil?
value
else
Expand Down
30 changes: 24 additions & 6 deletions spec/integration/type_converters/type_converters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
Neo4j::TypeConverters.converters[Date].should eq(Neo4j::TypeConverters::DateConverter)
end

it 'has converters for Hash' do
Neo4j::TypeConverters.converters[Hash].should eq(Neo4j::TypeConverters::HashConverter)
it 'has converters for JSON' do
Neo4j::TypeConverters.converters[JSON].should eq(Neo4j::TypeConverters::JSONConverter)
end

it 'has converters for YAML' do
Neo4j::TypeConverters.converters[Hash].should eq(Neo4j::TypeConverters::YAMLConverter)
end
end

Expand Down Expand Up @@ -48,20 +52,34 @@
end
end

describe Neo4j::TypeConverters::HashConverter do
subject { Neo4j::TypeConverters::HashConverter }
describe Neo4j::TypeConverters::JSONConverter do
subject { Neo4j::TypeConverters::JSONConverter }

let(:links) { {neo4j: 'http://www.neo4j.org', neotech: 'http://www.neotechnology.com/' } }

it 'translates from and to database' do
db_value = Neo4j::TypeConverters::HashConverter.to_db(links)
ruby_value = Neo4j::TypeConverters::HashConverter.to_ruby(db_value)
db_value = Neo4j::TypeConverters::JSONConverter.to_db(links)
ruby_value = Neo4j::TypeConverters::JSONConverter.to_ruby(db_value)
db_value.class.should eq String
ruby_value.class.should eq Hash
ruby_value['neo4j'].should eq 'http://www.neo4j.org'
end
end

describe Neo4j::TypeConverters::YAMLConverter do
subject { Neo4j::TypeConverters::YAMLConverter }

let(:links) { {neo4j: 'http://www.neo4j.org', neotech: 'http://www.neotechnology.com/' } }

it 'translates from and to database' do
db_value = Neo4j::TypeConverters::YAMLConverter.to_db(links)
ruby_value = Neo4j::TypeConverters::YAMLConverter.to_ruby(db_value)
db_value.class.should eq String
ruby_value.class.should eq Hash
ruby_value[:neo4j].should eq 'http://www.neo4j.org'
end
end

describe Neo4j::TypeConverters::DateConverter do
subject { Neo4j::TypeConverters::DateConverter }

Expand Down

1 comment on commit 0de9f0e

@subvertallchris
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://github.com/andreasronge/neo4j/wiki/Neo4j-v3#property-serialization

In short, it serializes as JSON by default; it uses YAML if you pass it the Hash constant. This is sort of the inverse of Rails, where YAML is the default and you pass JSON if you want to go that way.

Please sign in to comment.