module ActiveGraph::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/active_graph/shared/attributes.rb 125 def attribute(name) 126 fail ActiveGraph::DangerousAttributeError, %(an attribute method named "#{name}" would conflict with an existing method) if dangerous_attribute?(name) 127 128 attribute!(name) 129 end
Returns an Array of attribute names as Strings
@example Get attribute names
Person.attribute_names
@return [Array<String>] The attribute names
# File lib/active_graph/shared/attributes.rb 137 def attribute_names 138 attributes.keys 139 end
Returns a Hash of AttributeDefinition instances
@example Get attribute definitions
Person.attributes
@return [ActiveSupport::HashWithIndifferentAccess{String => ActiveGraph::Shared::AttributeDefinition}]
The Hash of AttributeDefinition instances
# File lib/active_graph/shared/attributes.rb 148 def attributes 149 @attributes ||= ActiveSupport::HashWithIndifferentAccess.new 150 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/active_graph/shared/attributes.rb 168 def dangerous_attribute?(name) 169 methods = instance_methods 170 171 attribute_methods(name).detect do |method_name| 172 !DEPRECATED_OBJECT_METHODS.include?(method_name.to_s) && methods.include?(method_name) 173 end unless attribute_names.include? name.to_s 174 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/active_graph/shared/attributes.rb 182 def inspect 183 inspected_attributes = attribute_names.sort 184 attributes_list = "(#{inspected_attributes.join(', ')})" unless inspected_attributes.empty? 185 "#{name}#{attributes_list}" 186 end
Protected Instance Methods
Assign a set of attribute definitions, used when subclassing models
@param [Array<ActiveGraph::Shared::DeclaredProperties>] attributes The Array of
AttributeDefinition instances
# File lib/active_graph/shared/attributes.rb 194 def attributes=(attributes) 195 @attributes = attributes 196 end
Overrides ActiveModel::AttributeMethods to backport 3.2 fix
# File lib/active_graph/shared/attributes.rb 199 def instance_method_already_implemented?(method_name) 200 generated_attribute_methods.method_defined?(method_name) 201 end
Private Instance Methods
Expand an attribute name into its generated methods names
# File lib/active_graph/shared/attributes.rb 206 def attribute_methods(name) 207 attribute_method_matchers.map { |matcher| matcher.method_name name } 208 end
Ruby inherited hook to assign superclass attributes to subclasses
# File lib/active_graph/shared/attributes.rb 211 def inherited(subclass) 212 super 213 subclass.attributes = attributes.dup 214 end