module Neo4j::Shared::Property::ClassMethods
Constants
- VALID_PROPERTY_OPTIONS
Public Instance Methods
attributes_nil_hash()
click to toggle source
@return [Hash] A frozen hash of all model properties with nil values. It is used during node loading and prevents an extra call to a slow dependency method.
# File lib/neo4j/shared/property.rb 200 def attributes_nil_hash 201 declared_properties.attributes_nil_hash 202 end
build_property(name, options) { |name| ... }
click to toggle source
# File lib/neo4j/shared/property.rb 172 def build_property(name, options) 173 decl_prop = DeclaredProperty.new(name, options).tap do |prop| 174 prop.register 175 declared_properties.register(prop) 176 yield name 177 constraint_or_index(name, options) 178 end 179 180 # If this class has already been inherited, make sure subclasses inherit property 181 subclasses.each do |klass| 182 klass.inherit_property name, decl_prop.clone, declared_properties[name].options 183 end 184 185 decl_prop 186 end
declared_properties()
click to toggle source
# File lib/neo4j/shared/property.rb 194 def declared_properties 195 @_declared_properties ||= DeclaredProperties.new(self) 196 end
extract_association_attributes!(props)
click to toggle source
# File lib/neo4j/shared/property.rb 204 def extract_association_attributes!(props) 205 props 206 end
inherit_property(name, attr_def, options = {})
click to toggle source
@param [Symbol] name The property name @param [Neo4j::Shared::AttributeDefinition] attr_def A cloned AttributeDefinition to reuse @param [Hash] options An options hash to use in the new property definition
# File lib/neo4j/shared/property.rb 166 def inherit_property(name, attr_def, options = {}) 167 build_property(name, options) do |prop_name| 168 attributes[prop_name] = attr_def 169 end 170 end
property(name, options = {})
click to toggle source
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.
@example Without type
class Person # declare a property which can have any value property :name end
@example With type and a default value
class Person # declare a property which can have any value property :score, type: Integer, default: 0 end
@example With an index
class Person # declare a property which can have any value property :name, index: :exact end
@example With a constraint
class Person # declare a property which can have any value property :name, constraint: :unique end
# File lib/neo4j/shared/property.rb 155 def property(name, options = {}) 156 invalid_option_keys = options.keys.map(&:to_sym) - VALID_PROPERTY_OPTIONS 157 fail ArgumentError, "Invalid options for property `#{name}` on `#{self.name}`: #{invalid_option_keys.join(', ')}" if invalid_option_keys.any? 158 build_property(name, options) do |prop| 159 attribute(prop) 160 end 161 end
undef_property(name)
click to toggle source
# File lib/neo4j/shared/property.rb 188 def undef_property(name) 189 undef_constraint_or_index(name) 190 declared_properties.unregister(name) 191 attribute_methods(name).each { |method| undef_method(method) } 192 end
Private Instance Methods
attribute!(name)
click to toggle source
Calls superclass method
# File lib/neo4j/shared/property.rb 210 def attribute!(name) 211 remove_instance_variable('@attribute_methods_generated') if instance_variable_defined?('@attribute_methods_generated') 212 define_attribute_methods([name]) unless attribute_names.include?(name) 213 attributes[name.to_s] = declared_properties[name] 214 define_method("#{name}=") do |value| 215 typecast_value = typecast_attribute(_attribute_typecaster(name), value) 216 send("#{name}_will_change!") unless typecast_value == read_attribute(name) 217 super(value) 218 end 219 end
constraint_or_index(name, options)
click to toggle source
# File lib/neo4j/shared/property.rb 221 def constraint_or_index(name, options) 222 # either constraint or index, do not set both 223 if options[:constraint] 224 fail "unknown constraint type #{options[:constraint]}, only :unique supported" if options[:constraint] != :unique 225 constraint(name, type: :unique) 226 elsif options[:index] 227 fail "unknown index type #{options[:index]}, only :exact supported" if options[:index] != :exact 228 index(name) if options[:index] == :exact 229 end 230 end
undef_constraint_or_index(name)
click to toggle source
# File lib/neo4j/shared/property.rb 232 def undef_constraint_or_index(name) 233 prop = declared_properties[name] 234 return unless prop.index_or_constraint? 235 type = prop.constraint? ? :constraint : :index 236 send(:"drop_#{type}", name) 237 end