module Neo4j::Shared::Property
Constants
- DATE_KEY_REGEX
Attributes
_persisted_obj[R]
Public Class Methods
new(attributes = nil)
click to toggle source
# File lib/neo4j/shared/property.rb 30 def initialize(attributes = nil) 31 attributes = process_attributes(attributes) 32 modded_attributes = inject_defaults!(attributes) 33 validate_attributes!(modded_attributes) 34 writer_method_props = extract_writer_methods!(modded_attributes) 35 send_props(writer_method_props) 36 self.undeclared_properties = attributes 37 @_persisted_obj = nil 38 end
Public Instance Methods
inject_defaults!(starting_props)
click to toggle source
# File lib/neo4j/shared/property.rb 42 def inject_defaults!(starting_props) 43 return starting_props if self.class.declared_properties.declared_property_defaults.empty? 44 self.class.declared_properties.inject_defaults!(self, starting_props || {}) 45 end
inspect()
click to toggle source
# File lib/neo4j/shared/property.rb 21 def inspect 22 attribute_descriptions = inspect_attributes.map do |key, value| 23 "#{Neo4j::ANSI::CYAN}#{key}: #{Neo4j::ANSI::CLEAR}#{value.inspect}" 24 end.join(', ') 25 26 separator = ' ' unless attribute_descriptions.empty? 27 "#<#{Neo4j::ANSI::YELLOW}#{self.class.name}#{Neo4j::ANSI::CLEAR}#{separator}#{attribute_descriptions}>" 28 end
mutations_from_database()
click to toggle source
TODO: Set @attribute correctly using class ActiveModel::Attribute, and after that remove mutations_from_database
and other ActiveModel::Dirty overrided methods
# File lib/neo4j/shared/property.rb 16 def mutations_from_database 17 @mutations_from_database ||= 18 defined?(ActiveModel::ForcedMutationTracker) ? ActiveModel::ForcedMutationTracker.new(self) : ActiveModel::NullMutationTracker.instance 19 end
read_attribute(name)
click to toggle source
# File lib/neo4j/shared/property.rb 47 def read_attribute(name) 48 respond_to?(name) ? send(name) : nil 49 end
Also aliased as: []
reload_properties!(properties)
click to toggle source
# File lib/neo4j/shared/property.rb 57 def reload_properties!(properties) 58 @attributes = nil 59 convert_and_assign_attributes(properties) 60 end
send_props(hash)
click to toggle source
# File lib/neo4j/shared/property.rb 52 def send_props(hash) 53 return hash if hash.blank? 54 hash.each { |key, value| send("#{key}=", value) } 55 end
undeclared_properties=(_)
click to toggle source
# File lib/neo4j/shared/property.rb 40 def undeclared_properties=(_); end
Private Instance Methods
extract_writer_methods!(attributes)
click to toggle source
# File lib/neo4j/shared/property.rb 74 def extract_writer_methods!(attributes) 75 return attributes if attributes.blank? 76 {}.tap do |writer_method_props| 77 attributes.each_key do |key| 78 writer_method_props[key] = attributes.delete(key) if self.respond_to?("#{key}=") 79 end 80 end 81 end
instantiate_object(field, values_with_empty_parameters)
click to toggle source
# File lib/neo4j/shared/property.rb 114 def instantiate_object(field, values_with_empty_parameters) 115 return nil if values_with_empty_parameters.all?(&:nil?) 116 values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v } 117 klass = field.type 118 klass ? klass.new(*values) : values 119 end
process_attributes(attributes = nil)
click to toggle source
Gives support for Rails
date_select, datetime_select, time_select helpers.
# File lib/neo4j/shared/property.rb 85 def process_attributes(attributes = nil) 86 return attributes if attributes.blank? 87 multi_parameter_attributes = {} 88 new_attributes = {} 89 attributes.each_pair do |key, value| 90 if key.match(DATE_KEY_REGEX) 91 match = key.to_s.match(DATE_KEY_REGEX) 92 found_key = match[1] 93 index = match[2].to_i 94 (multi_parameter_attributes[found_key] ||= {})[index] = value.empty? ? nil : value.send("to_#{$3}") 95 else 96 new_attributes[key] = value 97 end 98 end 99 100 multi_parameter_attributes.empty? ? new_attributes : process_multiparameter_attributes(multi_parameter_attributes, new_attributes) 101 end
process_multiparameter_attributes(multi_parameter_attributes, new_attributes)
click to toggle source
# File lib/neo4j/shared/property.rb 103 def process_multiparameter_attributes(multi_parameter_attributes, new_attributes) 104 multi_parameter_attributes.each_with_object(new_attributes) do |(key, values), attributes| 105 values = (values.keys.min..values.keys.max).map { |i| values[i] } 106 if (field = self.class.attributes[key.to_sym]).nil? 107 fail MultiparameterAssignmentError, "error on assignment #{values.inspect} to #{key}" 108 end 109 110 attributes[key] = instantiate_object(field, values) 111 end 112 end
validate_attributes!(attributes)
click to toggle source
Changes attributes hash to remove relationship keys Raises an error if there are any keys left which haven't been defined as properties on the model TODO: use declared_properties instead of self.attributes
# File lib/neo4j/shared/property.rb 67 def validate_attributes!(attributes) 68 return attributes if attributes.blank? 69 invalid_properties = attributes.keys.map(&:to_s) - self.attributes.keys 70 invalid_properties.reject! { |name| self.respond_to?("#{name}=") } 71 fail UndefinedPropertyError, "Undefined properties: #{invalid_properties.join(',')}" if !invalid_properties.empty? 72 end