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 70 def query_attribute(name) 71 fail Neo4j::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/neo4j/shared/attributes.rb 63 def write_attribute(name, value) 64 fail Neo4j::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/neo4j/shared/attributes.rb 79 def attribute(name) 80 @attributes ||= {} 81 @attributes[name] 82 end
Write an attribute to the attributes hash
# File lib/neo4j/shared/attributes.rb 85 def attribute=(name, value) 86 @attributes ||= {} 87 @attributes[name] = value 88 end
# File lib/neo4j/shared/attributes.rb 103 def attribute?(name) 104 Neo4j::Shared::TypeConverters::BooleanConverter.to_ruby(read_attribute(name)) 105 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 99 def attributes_map 100 Hash[self.class.attribute_names.map { |name| [name, yield(name)] }] 101 end