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

==(other) click to toggle source

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
[]=(name, value)
Alias for: write_attribute
attributes() click to toggle source

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
query_attribute(name) click to toggle source
   # 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_attribute(name, value) click to toggle source

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
Also aliased as: []=

Private Instance Methods

attribute(name) click to toggle source

Read an attribute from the attributes hash

   # File lib/neo4j/shared/attributes.rb
83 def attribute(name)
84   @attributes ||= {}
85   @attributes[name]
86 end
attribute=(name, value) click to toggle source

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
attribute?(name) click to toggle source
    # File lib/neo4j/shared/attributes.rb
107 def attribute?(name)
108   Neo4j::Shared::TypeConverters::BooleanConverter.to_ruby(read_attribute(name))
109 end
attributes_map() click to toggle source

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