class ActiveGraph::Shared::DeclaredProperty

Contains methods related to the management

Constants

ILLEGAL_PROPS

Attributes

default[R]
default_value[R]
magic_typecaster[R]
name[R]
name_string[R]
name_sym[R]
options[R]
type[R]
typecaster[R]

Public Class Methods

new(name, options = {}) click to toggle source
   # File lib/active_graph/shared/declared_property.rb
13 def initialize(name, options = {})
14   fail IllegalPropertyError, "#{name} is an illegal property" if ILLEGAL_PROPS.include?(name.to_s)
15   fail TypeError, "can't convert #{name.class} into Symbol" unless name.respond_to?(:to_sym)
16   @name = @name_sym = name.to_sym
17   @name_string = name.to_s
18   @type = options[:type]
19   @typecaster = options[:typecaster]
20   @default_value = options[:default]
21   @options = options
22   fail_invalid_options!
23 end

Public Instance Methods

<=>(other) click to toggle source

Compare attribute definitions

@example

attribute_definition <=> other

@param [ActiveGraph::Shared::DeclaredProperty, Object] other The other

attribute definition to compare with.

@return [-1, 0, 1, nil]

   # File lib/active_graph/shared/declared_property.rb
34 def <=>(other)
35   return nil unless other.instance_of? self.class
36   return nil if name == other.name && options != other.options
37   self.to_s <=> other.to_s
38 end
[](key) click to toggle source
   # File lib/active_graph/shared/declared_property.rb
54 def [](key)
55   respond_to?(key) ? public_send(key) : nil
56 end
fail_invalid_options!() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
62 def fail_invalid_options!
63   case
64   when index?(:exact) && constraint?(:unique)
65     fail ActiveGraph::InvalidPropertyOptionsError,
66          "#Uniqueness constraints also provide exact indexes, cannot set both options on property #{name}"
67   end
68 end
inspect() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
40 def inspect
41   options_description = options.map { |key, value| "#{key.inspect} => #{value.inspect}" }.sort.join(', ')
42   inspected_options = ", #{options_description}" unless options_description.empty?
43   "attribute :#{name}#{inspected_options}"
44 end
register() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
58 def register
59   register_magic_properties
60 end
to_s() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
46 def to_s
47   name.to_s
48 end
to_sym() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
50 def to_sym
51   name
52 end

Private Instance Methods

option_with_value!(key, value) click to toggle source
   # File lib/active_graph/shared/declared_property.rb
72 def option_with_value!(key, value)
73   options[key] = value
74   fail_invalid_options!
75 end
option_with_value?(key, value) click to toggle source
   # File lib/active_graph/shared/declared_property.rb
77 def option_with_value?(key, value)
78   options[key] == value
79 end
register_magic_properties() click to toggle source

Tweaks properties

   # File lib/active_graph/shared/declared_property.rb
82 def register_magic_properties
83   @type ||= ActiveGraph::Config.timestamp_type if timestamp_prop?
84 
85   register_magic_typecaster
86   register_type_converter
87 end
register_magic_typecaster() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
93 def register_magic_typecaster
94   found_typecaster = ActiveGraph::Shared::TypeConverters.typecaster_for(type)
95   return unless found_typecaster && found_typecaster.respond_to?(:primitive_type)
96   @typecaster = found_typecaster
97   @magic_typecaster = type
98   @type = found_typecaster.primitive_type
99 end
register_type_converter() click to toggle source
    # File lib/active_graph/shared/declared_property.rb
101 def register_type_converter
102   converter = options[:serializer]
103   return unless converter
104   @type        = converter.convert_type
105   @typecaster  = ActiveGraph::Shared::TypeConverters::ObjectConverter
106   ActiveGraph::Shared::TypeConverters.register_converter(converter)
107 end
timestamp_prop?() click to toggle source
   # File lib/active_graph/shared/declared_property.rb
89 def timestamp_prop?
90   name.to_sym == :created_at || name.to_sym == :updated_at
91 end