Module: ActiveGraph::Shared::Property::ClassMethods

Extended by:
Forwardable
Defined in:
lib/active_graph/shared/property.rb

Constant Summary collapse

VALID_PROPERTY_OPTIONS =
%w(type default index constraint serializer typecaster).map(&:to_sym)

Instance Method Summary collapse

Instance Method Details

#attributes_nil_hashHash

an extra call to a slow dependency method.

Returns:

  • (Hash)

    A frozen hash of all model properties with nil values. It is used during node loading and prevents


205
206
207
# File 'lib/active_graph/shared/property.rb', line 205

def attributes_nil_hash
  declared_properties.attributes_nil_hash
end

#build_property(name, options) ⇒ Object


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/active_graph/shared/property.rb', line 177

def build_property(name, options)
  decl_prop = DeclaredProperty.new(name, options).tap do |prop|
    prop.register
    declared_properties.register(prop)
    yield name
    constraint_or_index(name, options)
  end

  # If this class has already been inherited, make sure subclasses inherit property
  subclasses.each do |klass|
    klass.inherit_property name, decl_prop.clone, declared_properties[name].options
  end

  decl_prop
end

#declared_propertiesObject


199
200
201
# File 'lib/active_graph/shared/property.rb', line 199

def declared_properties
  @_declared_properties ||= DeclaredProperties.new(self)
end

#extract_association_attributes!(props) ⇒ Object


209
210
211
# File 'lib/active_graph/shared/property.rb', line 209

def extract_association_attributes!(props)
  props
end

#inherit_property(name, attr_def, options = {}) ⇒ Object

Parameters:

  • name (Symbol)

    The property name

  • attr_def (ActiveGraph::Shared::AttributeDefinition)

    A cloned AttributeDefinition to reuse

  • options (Hash) (defaults to: {})

    An options hash to use in the new property definition


171
172
173
174
175
# File 'lib/active_graph/shared/property.rb', line 171

def inherit_property(name, attr_def, options = {})
  build_property(name, options) do |prop_name|
    attributes[prop_name] = attr_def
  end
end

#property(name, options = {}) ⇒ Object

Defines a property on the class

See active_attr gem for allowed options, e.g which type Notice, in ActiveGraph you don't have to declare properties before using them, see the ActiveGraph::Coree api.

Examples:

Without type

class Person
  # declare a property which can have any value
  property :name
end

With type and a default value

class Person
  # declare a property which can have any value
  property :score, type: Integer, default: 0
end

With an index

class Person
  # declare a property which can have any value
  property :name, index: :exact
end

With a constraint

class Person
  # declare a property which can have any value
  property :name, constraint: :unique
end

160
161
162
163
164
165
166
# File 'lib/active_graph/shared/property.rb', line 160

def property(name, options = {})
  invalid_option_keys = options.keys.map(&:to_sym) - VALID_PROPERTY_OPTIONS
  fail ArgumentError, "Invalid options for property `#{name}` on `#{self.name}`: #{invalid_option_keys.join(', ')}" if invalid_option_keys.any?
  build_property(name, options) do |prop|
    attribute(prop)
  end
end

#undef_property(name) ⇒ Object


193
194
195
196
197
# File 'lib/active_graph/shared/property.rb', line 193

def undef_property(name)
  undef_constraint_or_index(name)
  declared_properties.unregister(name)
  attribute_methods(name).each { |method| undef_method(method) }
end