module ActiveGraph::Shared::Attributes
Attributes
provides a set of class methods for defining an attributes schema and instance methods for reading and writing attributes.
@example Usage
class Person include ActiveGraph::Shared::Attributes attribute :name end person = Person.new person.name = "Ben Poweski"
Originally part of ActiveAttr, github.com/cgriego/active_attr
Constants
- DEPRECATED_OBJECT_METHODS
Methods deprecated on the
Object
class which can be safely overridden
Public Instance Methods
Performs equality checking on the result of attributes and its type.
@example Compare for equality.
model == other
@param [ActiveAttr::Attributes, Object] other The other model to compare
@return [true, false] True if attributes are equal and other is instance
of the same Class, false if not.
# File lib/active_graph/shared/attributes.rb 37 def ==(other) 38 return false unless other.instance_of? self.class 39 attributes == other.attributes 40 end
Returns a Hash of all attributes
@example Get attributes
person.attributes # => {"name"=>"Ben Poweski"}
@return [Hash{String => Object}] The Hash of all attributes
# File lib/active_graph/shared/attributes.rb 48 def attributes 49 attributes_map { |name| send name } 50 end
# File lib/active_graph/shared/attributes.rb 70 def query_attribute(name) 71 fail ActiveGraph::UnknownAttributeError, "unknown attribute: #{name}" if !respond_to? "#{name}?" 72 73 send "#{name}?" 74 end
Write a single attribute to the model's attribute hash.
@example Write the attribute with write_attribute
person.write_attribute(:name, "Benjamin")
@example Write an attribute with bracket syntax
person[:name] = "Benjamin"
@param [String, Symbol, to_s] name The name of the attribute to update. @param [Object] value The value to set for the attribute.
@raise [UnknownAttributeError] if the attribute is unknown
# File lib/active_graph/shared/attributes.rb 63 def write_attribute(name, value) 64 fail ActiveGraph::UnknownAttributeError, "unknown attribute: #{name}" if !respond_to? "#{name}=" 65 66 send "#{name}=", value 67 end
Private Instance Methods
Read an attribute from the attributes hash
# File lib/active_graph/shared/attributes.rb 79 def attribute(name) 80 @attributes ||= ActiveGraph::AttributeSet.new({}, self.class.attributes.keys) 81 @attributes.fetch_value(name.to_s) 82 end
Write an attribute to the attributes hash
# File lib/active_graph/shared/attributes.rb 85 def attribute=(name, value) 86 @attributes ||= ActiveGraph::AttributeSet.new({}, self.class.attributes.keys) 87 @attributes.write_cast_value(name, value) 88 value 89 end
# File lib/active_graph/shared/attributes.rb 104 def attribute?(name) 105 ActiveGraph::Shared::TypeConverters::BooleanConverter.to_ruby(read_attribute(name)) 106 end
Maps all attributes using the given block
@example Stringify attributes
person.attributes_map { |name| send(name).to_s }
@yield [name] block called to return hash value @yieldparam [String] name The name of the attribute to map.
@return [Hash{String => Object}] The Hash of mapped attributes
# File lib/active_graph/shared/attributes.rb 100 def attributes_map 101 Hash[self.class.attribute_names.map { |name| [name, yield(name)] }] 102 end