module Neo4j::Shared::Attributes::ClassMethods
Public Instance Methods
Defines an attribute
For each attribute that is defined, a getter and setter will be added as an instance method to the model. An {AttributeDefinition} instance will be added to result of the attributes class method.
@example Define an attribute.
attribute :name
@param (see AttributeDefinition#initialize)
@raise [DangerousAttributeError] if the attribute name conflicts with
existing methods
@return [AttributeDefinition] Attribute’s definition
# File lib/neo4j/shared/attributes.rb 128 def attribute(name) 129 if dangerous_attribute?(name) 130 fail Neo4j::DangerousAttributeError, %(an attribute method named "#{name}" would conflict with an existing method) 131 else 132 attribute!(name) 133 end 134 end
Returns an Array of attribute names as Strings
@example Get attribute names
Person.attribute_names
@return [Array<String>] The attribute names
# File lib/neo4j/shared/attributes.rb 142 def attribute_names 143 attributes.keys 144 end
Returns a Hash of AttributeDefinition instances
@example Get attribute definitions
Person.attributes
@return [ActiveSupport::HashWithIndifferentAccess{String => Neo4j::Shared::AttributeDefinition}]
The Hash of AttributeDefinition instances
# File lib/neo4j/shared/attributes.rb 153 def attributes 154 @attributes ||= ActiveSupport::HashWithIndifferentAccess.new 155 end
Determine if a given attribute name is dangerous
Some attribute names can cause conflicts with existing methods on an object. For example, an attribute named “timeout” would conflict with the timeout method that Ruby’s Timeout library mixes into Object.
@example Testing a harmless attribute
Person.dangerous_attribute? :name #=> false
@example Testing a dangerous attribute
Person.dangerous_attribute? :nil #=> "nil?"
@param name Attribute name
@return [false, String] False or the conflicting method name
# File lib/neo4j/shared/attributes.rb 173 def dangerous_attribute?(name) 174 attribute_methods(name).detect do |method_name| 175 !DEPRECATED_OBJECT_METHODS.include?(method_name.to_s) && allocate.respond_to?(method_name, true) 176 end unless attribute_names.include? name.to_s 177 end
Returns the class name plus its attribute names
@example Inspect the model’s definition.
Person.inspect
@return [String] Human-readable presentation of the attributes
# File lib/neo4j/shared/attributes.rb 185 def inspect 186 inspected_attributes = attribute_names.sort 187 attributes_list = "(#{inspected_attributes.join(', ')})" unless inspected_attributes.empty? 188 "#{name}#{attributes_list}" 189 end
Protected Instance Methods
Assign a set of attribute definitions, used when subclassing models
@param [Array<Neo4j::Shared::DeclaredProperties>] The Array of
AttributeDefinition instances
# File lib/neo4j/shared/attributes.rb 197 def attributes=(attributes) 198 @attributes = attributes 199 end
Overrides ActiveModel::AttributeMethods to backport 3.2 fix
# File lib/neo4j/shared/attributes.rb 202 def instance_method_already_implemented?(method_name) 203 generated_attribute_methods.method_defined?(method_name) 204 end
Private Instance Methods
Expand an attribute name into its generated methods names
# File lib/neo4j/shared/attributes.rb 209 def attribute_methods(name) 210 attribute_method_matchers.map { |matcher| matcher.method_name name } 211 end
Ruby inherited hook to assign superclass attributes to subclasses
# File lib/neo4j/shared/attributes.rb 214 def inherited(subclass) 215 super 216 subclass.attributes = attributes.dup 217 end