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 23 def initialize(attributes = nil) 24 attributes = process_attributes(attributes) 25 modded_attributes = inject_defaults!(attributes) 26 validate_attributes!(modded_attributes) 27 writer_method_props = extract_writer_methods!(modded_attributes) 28 send_props(writer_method_props) 29 @_persisted_obj = nil 30 end
Public Instance Methods
inject_defaults!(starting_props)
click to toggle source
# File lib/neo4j/shared/property.rb 32 def inject_defaults!(starting_props) 33 return starting_props if self.class.declared_properties.declared_property_defaults.empty? 34 self.class.declared_properties.inject_defaults!(self, starting_props || {}) 35 end
inspect()
click to toggle source
# File lib/neo4j/shared/property.rb 14 def inspect 15 attribute_descriptions = inspect_attributes.map do |key, value| 16 "#{Neo4j::ANSI::CYAN}#{key}: #{Neo4j::ANSI::CLEAR}#{value.inspect}" 17 end.join(', ') 18 19 separator = ' ' unless attribute_descriptions.empty? 20 "#<#{Neo4j::ANSI::YELLOW}#{self.class.name}#{Neo4j::ANSI::CLEAR}#{separator}#{attribute_descriptions}>" 21 end
read_attribute(name)
click to toggle source
# File lib/neo4j/shared/property.rb 37 def read_attribute(name) 38 respond_to?(name) ? send(name) : nil 39 end
Also aliased as: []
reload_properties!(properties)
click to toggle source
# File lib/neo4j/shared/property.rb 47 def reload_properties!(properties) 48 @attributes = nil 49 convert_and_assign_attributes(properties) 50 end
send_props(hash)
click to toggle source
# File lib/neo4j/shared/property.rb 42 def send_props(hash) 43 return hash if hash.blank? 44 hash.each { |key, value| send("#{key}=", value) } 45 end
Private Instance Methods
extract_writer_methods!(attributes)
click to toggle source
# File lib/neo4j/shared/property.rb 64 def extract_writer_methods!(attributes) 65 return attributes if attributes.blank? 66 {}.tap do |writer_method_props| 67 attributes.each_key do |key| 68 writer_method_props[key] = attributes.delete(key) if self.respond_to?("#{key}=") 69 end 70 end 71 end
instantiate_object(field, values_with_empty_parameters)
click to toggle source
# File lib/neo4j/shared/property.rb 104 def instantiate_object(field, values_with_empty_parameters) 105 return nil if values_with_empty_parameters.all?(&:nil?) 106 values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v } 107 klass = field.type 108 klass ? klass.new(*values) : values 109 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 75 def process_attributes(attributes = nil) 76 return attributes if attributes.blank? 77 multi_parameter_attributes = {} 78 new_attributes = {} 79 attributes.each_pair do |key, value| 80 if key.match(DATE_KEY_REGEX) 81 match = key.to_s.match(DATE_KEY_REGEX) 82 found_key = match[1] 83 index = match[2].to_i 84 (multi_parameter_attributes[found_key] ||= {})[index] = value.empty? ? nil : value.send("to_#{$3}") 85 else 86 new_attributes[key] = value 87 end 88 end 89 90 multi_parameter_attributes.empty? ? new_attributes : process_multiparameter_attributes(multi_parameter_attributes, new_attributes) 91 end
process_multiparameter_attributes(multi_parameter_attributes, new_attributes)
click to toggle source
# File lib/neo4j/shared/property.rb 93 def process_multiparameter_attributes(multi_parameter_attributes, new_attributes) 94 multi_parameter_attributes.each_with_object(new_attributes) do |(key, values), attributes| 95 values = (values.keys.min..values.keys.max).map { |i| values[i] } 96 if (field = self.class.attributes[key.to_sym]).nil? 97 fail MultiparameterAssignmentError, "error on assignment #{values.inspect} to #{key}" 98 end 99 100 attributes[key] = instantiate_object(field, values) 101 end 102 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 57 def validate_attributes!(attributes) 58 return attributes if attributes.blank? 59 invalid_properties = attributes.keys.map(&:to_s) - self.attributes.keys 60 invalid_properties.reject! { |name| self.respond_to?("#{name}=") } 61 fail UndefinedPropertyError, "Undefined properties: #{invalid_properties.join(',')}" if invalid_properties.size > 0 62 end