Module: Neo4j::Shared::Property::ClassMethods

Extended by:
Forwardable
Defined in:
lib/neo4j/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



200
201
202
# File 'lib/neo4j/shared/property.rb', line 200

def attributes_nil_hash
  declared_properties.attributes_nil_hash
end

#build_property(name, options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/neo4j/shared/property.rb', line 172

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



194
195
196
# File 'lib/neo4j/shared/property.rb', line 194

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

#extract_association_attributes!(props) ⇒ Object



204
205
206
# File 'lib/neo4j/shared/property.rb', line 204

def extract_association_attributes!(props)
  props
end

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

Parameters:

  • name (Symbol)

    The property name

  • attr_def (Neo4j::Shared::AttributeDefinition)

    A cloned AttributeDefinition to reuse

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

    An options hash to use in the new property definition



166
167
168
169
170
# File 'lib/neo4j/shared/property.rb', line 166

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 Neo4j you don’t have to declare properties before using them, see the neo4j-core 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


155
156
157
158
159
160
161
# File 'lib/neo4j/shared/property.rb', line 155

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



188
189
190
191
192
# File 'lib/neo4j/shared/property.rb', line 188

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