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 124 def attribute(name) 125 fail Neo4j::DangerousAttributeError, %(an attribute method named "#{name}" would conflict with an existing method) if dangerous_attribute?(name) 126 127 attribute!(name) 128 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 136 def attribute_names 137 attributes.keys 138 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 147 def attributes 148 @attributes ||= ActiveSupport::HashWithIndifferentAccess.new 149 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 167 def dangerous_attribute?(name) 168 methods = instance_methods 169 170 attribute_methods(name).detect do |method_name| 171 !DEPRECATED_OBJECT_METHODS.include?(method_name.to_s) && methods.include?(method_name) 172 end unless attribute_names.include? name.to_s 173 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 181 def inspect 182 inspected_attributes = attribute_names.sort 183 attributes_list = "(#{inspected_attributes.join(', ')})" unless inspected_attributes.empty? 184 "#{name}#{attributes_list}" 185 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 193 def attributes=(attributes) 194 @attributes = attributes 195 end
Overrides ActiveModel::AttributeMethods to backport 3.2 fix
# File lib/neo4j/shared/attributes.rb 198 def instance_method_already_implemented?(method_name) 199 generated_attribute_methods.method_defined?(method_name) 200 end
Private Instance Methods
Expand an attribute name into its generated methods names
# File lib/neo4j/shared/attributes.rb 205 def attribute_methods(name) 206 attribute_method_matchers.map { |matcher| matcher.method_name name } 207 end
Ruby inherited hook to assign superclass attributes to subclasses
# File lib/neo4j/shared/attributes.rb 210 def inherited(subclass) 211 super 212 subclass.attributes = attributes.dup 213 end