module ActiveGraph::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/active_graph/shared/property.rb 205 def attributes_nil_hash 206 declared_properties.attributes_nil_hash 207 end
build_property(name, options) { |name| ... }
click to toggle source
# File lib/active_graph/shared/property.rb 177 def build_property(name, options) 178 decl_prop = DeclaredProperty.new(name, options).tap do |prop| 179 prop.register 180 declared_properties.register(prop) 181 yield name 182 constraint_or_index(name, options) 183 end 184 185 # If this class has already been inherited, make sure subclasses inherit property 186 subclasses.each do |klass| 187 klass.inherit_property name, decl_prop.clone, declared_properties[name].options 188 end 189 190 decl_prop 191 end
declared_properties()
click to toggle source
# File lib/active_graph/shared/property.rb 199 def declared_properties 200 @_declared_properties ||= DeclaredProperties.new(self) 201 end
extract_association_attributes!(props)
click to toggle source
# File lib/active_graph/shared/property.rb 209 def extract_association_attributes!(props) 210 props 211 end
inherit_property(name, attr_def, options = {})
click to toggle source
@param [Symbol] name The property name @param [ActiveGraph::Shared::AttributeDefinition] attr_def A cloned AttributeDefinition to reuse @param [Hash] options An options hash to use in the new property definition
# File lib/active_graph/shared/property.rb 171 def inherit_property(name, attr_def, options = {}) 172 build_property(name, options) do |prop_name| 173 attributes[prop_name] = attr_def 174 end 175 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 ActiveGraph
you don't have to declare properties before using them, see the ActiveGraph::Coree 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/active_graph/shared/property.rb 160 def property(name, options = {}) 161 invalid_option_keys = options.keys.map(&:to_sym) - VALID_PROPERTY_OPTIONS 162 fail ArgumentError, "Invalid options for property `#{name}` on `#{self.name}`: #{invalid_option_keys.join(', ')}" if invalid_option_keys.any? 163 build_property(name, options) do |prop| 164 attribute(prop) 165 end 166 end
undef_property(name)
click to toggle source
# File lib/active_graph/shared/property.rb 193 def undef_property(name) 194 undef_constraint_or_index(name) 195 declared_properties.unregister(name) 196 attribute_methods(name).each { |method| undef_method(method) } 197 end
Private Instance Methods
attribute!(name)
click to toggle source
Calls superclass method
# File lib/active_graph/shared/property.rb 215 def attribute!(name) 216 remove_instance_variable('@attribute_methods_generated') if instance_variable_defined?('@attribute_methods_generated') 217 define_attribute_methods([name]) unless attribute_names.include?(name) 218 attributes[name.to_s] = declared_properties[name] 219 define_method("#{name}=") do |value| 220 typecast_value = if NEO4J_DRIVER_DATA_TYPES.include?(_attribute_type(name)) 221 value 222 else 223 typecast_attribute(_attribute_typecaster(name), value) 224 end 225 send("#{name}_will_change!") unless typecast_value == read_attribute(name) 226 super(value) 227 end 228 end
constraint_or_index(name, options)
click to toggle source
# File lib/active_graph/shared/property.rb 230 def constraint_or_index(name, options) 231 # either constraint or index, do not set both 232 if options[:constraint] 233 fail "unknown constraint type #{options[:constraint]}, only :unique supported" if options[:constraint] != :unique 234 constraint(name, type: :unique) 235 elsif options[:index] 236 fail "unknown index type #{options[:index]}, only :exact supported" if options[:index] != :exact 237 index(name) if options[:index] == :exact 238 end 239 end
undef_constraint_or_index(name)
click to toggle source
# File lib/active_graph/shared/property.rb 241 def undef_constraint_or_index(name) 242 prop = declared_properties[name] 243 return unless prop.index_or_constraint? 244 type = prop.constraint? ? :constraint : :index 245 send(:"drop_#{type}", name) 246 end