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.


197
198
199
# File 'lib/neo4j/shared/property.rb', line 197

def attributes_nil_hash
  declared_properties.attributes_nil_hash
end

#build_property(name, options) ⇒ Object


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

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


191
192
193
# File 'lib/neo4j/shared/property.rb', line 191

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

#extract_association_attributes!(props) ⇒ Object


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

def extract_association_attributes!(props)
  props
end

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


163
164
165
166
167
# File 'lib/neo4j/shared/property.rb', line 163

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

152
153
154
155
156
157
158
# File 'lib/neo4j/shared/property.rb', line 152

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


185
186
187
188
189
# File 'lib/neo4j/shared/property.rb', line 185

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