-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started separating property management logic
- Loading branch information
1 parent
cd9bc9b
commit 56f1cb5
Showing
14 changed files
with
212 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Neo4j::Shared | ||
# Contains methods related to the management | ||
class DeclaredProperty | ||
class IllegalPropertyError < StandardError; end | ||
|
||
ILLEGAL_PROPS = %w(from_node to_node start_node end_node) | ||
attr_reader :name, :name_string, :name_sym, :options, :magic_typecaster | ||
|
||
def initialize(name, options) | ||
fail IllegalPropertyError, "#{name} is an illegal property" if ILLEGAL_PROPS.include?(name.to_s) | ||
@name = @name_sym = name | ||
@name_string = name.to_s | ||
@options = options | ||
end | ||
|
||
def register | ||
register_magic_properties | ||
end | ||
|
||
def type | ||
options[:type] | ||
end | ||
|
||
def typecaster | ||
options[:typecaster] | ||
end | ||
|
||
def default_value | ||
options[:default] | ||
end | ||
|
||
private | ||
|
||
# Tweaks properties | ||
def register_magic_properties | ||
options[:type] ||= DateTime if name.to_sym == :created_at || name.to_sym == :updated_at | ||
# TODO: Custom typecaster to fix the stuff below | ||
# ActiveAttr does not handle "Time", Rails and Neo4j.rb 2.3 did | ||
# Convert it to DateTime in the interest of consistency | ||
options[:type] = DateTime if options[:type] == Time | ||
|
||
register_magic_typecaster | ||
register_type_converter | ||
end | ||
|
||
def register_magic_typecaster | ||
found_typecaster = Neo4j::Shared::TypeConverters.typecaster_for(options[:type]) | ||
return unless found_typecaster && found_typecaster.respond_to?(:primitive_type) | ||
options[:typecaster] = found_typecaster | ||
@magic_typecaster = options[:type] | ||
options[:type] = found_typecaster.primitive_type | ||
end | ||
|
||
def register_type_converter | ||
converter = options[:serializer] | ||
return unless converter | ||
options[:type] = converter.convert_type | ||
options[:typecaster] = ActiveAttr::Typecasting::ObjectTypecaster.new | ||
Neo4j::Shared::TypeConverters.register_converter(converter) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
module Neo4j::Shared | ||
class DeclaredPropertyManager | ||
include Neo4j::Shared::TypeConverters | ||
|
||
attr_reader :klass | ||
|
||
def initialize(klass) | ||
@klass = klass | ||
end | ||
|
||
def register(property) | ||
@_attributes_nil_hash = nil | ||
registered_properties[property.name] = property | ||
register_magic_typecaster(property) if property.magic_typecaster | ||
declared_property_defaults[property.name] = property.default_value if property.default_value | ||
end | ||
|
||
def declared_property_defaults | ||
@_default_property_values ||= {} | ||
end | ||
|
||
def registered_properties | ||
@_registered_properties ||= {} | ||
end | ||
|
||
def attributes_nil_hash | ||
@_attributes_nil_hash ||= {}.tap { |attr_hash| registered_properties.each_pair { |k, _v| attr_hash[k.to_s] = nil } }.freeze | ||
end | ||
|
||
def unregister(name) | ||
# might need to be include?(name.to_s) | ||
fail ArgumentError, "Argument `#{name}` not an attribute" if not registered_properties[name] | ||
declared_prop = registered_properties[name] | ||
registered_properties.delete(declared_prop) | ||
unregister_magic_typecaster(name) | ||
unregister_property_default(name) | ||
end | ||
|
||
def serialize(name, coder = JSON) | ||
@serialize ||= {} | ||
@serialize[name] = coder | ||
end | ||
|
||
def serialized_properties=(serialize_hash) | ||
@serialized_property_keys = nil | ||
@serialize = serialize_hash.clone | ||
end | ||
|
||
def serialized_properties | ||
@serialize ||= {} | ||
end | ||
|
||
def serialized_properties_keys | ||
@serialized_property_keys ||= serialized_properties.keys | ||
end | ||
|
||
def magic_typecast_properties_keys | ||
@magic_typecast_properties_keys ||= magic_typecast_properties.keys | ||
end | ||
|
||
def magic_typecast_properties | ||
@magic_typecast_properties ||= {} | ||
end | ||
|
||
# The known mappings of declared properties and their primitive types. | ||
def upstream_primitives | ||
@upstream_primitives ||= {} | ||
end | ||
|
||
protected | ||
|
||
# Prevents repeated calls to :_attribute_type, which isn't free and never changes. | ||
def fetch_upstream_primitive(attr) | ||
upstream_primitives[attr] || upstream_primitives[attr] = klass._attribute_type(attr) | ||
end | ||
|
||
private | ||
|
||
def unregister_magic_typecaster(property) | ||
magic_typecast_properties.delete(property) | ||
@magic_typecast_properties_keys = nil | ||
end | ||
|
||
def unregister_property_default(property) | ||
declared_property_defaults.delete(property) | ||
@_default_property_values = nil | ||
end | ||
|
||
def register_magic_typecaster(property) | ||
magic_typecast_properties[property.name] = property.magic_typecaster | ||
@magic_typecast_properties_keys = nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.