module Neo4j::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 Neo4j::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/neo4j/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/neo4j/shared/attributes.rb 48 def attributes 49 attributes_map { |name| send name } 50 end
# File lib/neo4j/shared/attributes.rb 72 def query_attribute(name) 73 if respond_to? "#{name}?" 74 send "#{name}?" 75 else 76 fail Neo4j::UnknownAttributeError, "unknown attribute: #{name}" 77 end 78 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/neo4j/shared/attributes.rb 63 def write_attribute(name, value) 64 if respond_to? "#{name}=" 65 send "#{name}=", value 66 else 67 fail Neo4j::UnknownAttributeError, "unknown attribute: #{name}" 68 end 69 end
Private Instance Methods
Read an attribute from the attributes hash
# File lib/neo4j/shared/attributes.rb 83 def attribute(name) 84 @attributes ||= {} 85 @attributes[name] 86 end
Write an attribute to the attributes hash
# File lib/neo4j/shared/attributes.rb 89 def attribute=(name, value) 90 @attributes ||= {} 91 @attributes[name] = value 92 end
# File lib/neo4j/shared/attributes.rb 107 def attribute?(name) 108 Neo4j::Shared::TypeConverters::BooleanConverter.to_ruby(read_attribute(name)) 109 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/neo4j/shared/attributes.rb 103 def attributes_map 104 Hash[self.class.attribute_names.map { |name| [name, yield(name)] }] 105 end