module ActiveGraph::Shared::Property

Constants

DATE_KEY_REGEX
NEO4J_DRIVER_DATA_TYPES

This list should not be statically created. All types which have converters should by type casted

Attributes

_persisted_obj[R]

Public Class Methods

new(attributes = nil) click to toggle source
   # File lib/active_graph/shared/property.rb
34 def initialize(attributes = nil)
35   @attributes ||= ActiveGraph::AttributeSet.new({}, self.class.attributes.keys)
36   attributes = process_attributes(attributes)
37   modded_attributes = inject_defaults!(attributes)
38   validate_attributes!(modded_attributes)
39   writer_method_props = extract_writer_methods!(modded_attributes)
40   send_props(writer_method_props)
41   self.undeclared_properties = attributes
42   @_persisted_obj = nil
43 end

Public Instance Methods

[](name)
Alias for: read_attribute
inject_defaults!(starting_props) click to toggle source
   # File lib/active_graph/shared/property.rb
47 def inject_defaults!(starting_props)
48   return starting_props if self.class.declared_properties.declared_property_defaults.empty?
49   self.class.declared_properties.inject_defaults!(self, starting_props || {})
50 end
inspect() click to toggle source
   # File lib/active_graph/shared/property.rb
25 def inspect
26   attribute_descriptions = inspect_attributes.map do |key, value|
27     "#{ActiveGraph::ANSI::CYAN}#{key}: #{ActiveGraph::ANSI::CLEAR}#{value.inspect}"
28   end.join(', ')
29 
30   separator = ' ' unless attribute_descriptions.empty?
31   "#<#{ActiveGraph::ANSI::YELLOW}#{self.class.name}#{ActiveGraph::ANSI::CLEAR}#{separator}#{attribute_descriptions}>"
32 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/active_graph/shared/property.rb
20 def mutations_from_database
21   @mutations_from_database ||=
22     defined?(ActiveModel::ForcedMutationTracker) ? ActiveModel::ForcedMutationTracker.new(self) : ActiveModel::NullMutationTracker.instance
23 end
read_attribute(name) click to toggle source
   # File lib/active_graph/shared/property.rb
52 def read_attribute(name)
53   respond_to?(name) ? send(name) : nil
54 end
Also aliased as: []
reload_properties!(properties) click to toggle source
   # File lib/active_graph/shared/property.rb
62 def reload_properties!(properties)
63   @attributes = nil
64   convert_and_assign_attributes(properties)
65 end
send_props(hash) click to toggle source
   # File lib/active_graph/shared/property.rb
57 def send_props(hash)
58   return hash if hash.blank?
59   hash.each { |key, value| send("#{key}=", value) }
60 end
undeclared_properties=(_) click to toggle source
   # File lib/active_graph/shared/property.rb
45 def undeclared_properties=(_); end

Private Instance Methods

extract_writer_methods!(attributes) click to toggle source
   # File lib/active_graph/shared/property.rb
79 def extract_writer_methods!(attributes)
80   return attributes if attributes.blank?
81   {}.tap do |writer_method_props|
82     attributes.keys.each do |key|
83       writer_method_props[key] = attributes.delete(key) if self.respond_to?("#{key}=")
84     end
85   end
86 end
instantiate_object(field, values_with_empty_parameters) click to toggle source
    # File lib/active_graph/shared/property.rb
119 def instantiate_object(field, values_with_empty_parameters)
120   return nil if values_with_empty_parameters.all?(&:nil?)
121   values = values_with_empty_parameters.collect { |v| v.nil? ? 1 : v }
122   klass = field.type
123   klass ? klass.new(*values) : values
124 end
process_attributes(attributes = nil) click to toggle source

Gives support for Rails date_select, datetime_select, time_select helpers.

    # File lib/active_graph/shared/property.rb
 90 def process_attributes(attributes = nil)
 91   return attributes if attributes.blank?
 92   multi_parameter_attributes = {}
 93   new_attributes = {}
 94   attributes.each_pair do |key, value|
 95     if key.match(DATE_KEY_REGEX)
 96       match = key.to_s.match(DATE_KEY_REGEX)
 97       found_key = match[1]
 98       index = match[2].to_i
 99       (multi_parameter_attributes[found_key] ||= {})[index] = value.empty? ? nil : value.send("to_#{$3}")
100     else
101       new_attributes[key] = value
102     end
103   end
104 
105   multi_parameter_attributes.empty? ? new_attributes : process_multiparameter_attributes(multi_parameter_attributes, new_attributes)
106 end
process_multiparameter_attributes(multi_parameter_attributes, new_attributes) click to toggle source
    # File lib/active_graph/shared/property.rb
108 def process_multiparameter_attributes(multi_parameter_attributes, new_attributes)
109   multi_parameter_attributes.each_with_object(new_attributes) do |(key, values), attributes|
110     values = (values.keys.min..values.keys.max).map { |i| values[i] }
111     if (field = self.class.attributes[key.to_sym]).nil?
112       fail MultiparameterAssignmentError, "error on assignment #{values.inspect} to #{key}"
113     end
114 
115     attributes[key] = instantiate_object(field, values)
116   end
117 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/active_graph/shared/property.rb
72 def validate_attributes!(attributes)
73   return attributes if attributes.blank?
74   invalid_properties = attributes.keys.map(&:to_s) - self.attributes.keys
75   invalid_properties.reject! { |name| self.respond_to?("#{name}=") }
76   fail UndefinedPropertyError, "Undefined properties: #{invalid_properties.join(',')}" if !invalid_properties.empty?
77 end