Module: Neo4j::Shared::Attributes::ClassMethods

Defined in:
lib/neo4j/shared/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name) ⇒ AttributeDefinition

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.

Examples:

Define an attribute.

attribute :name

Returns:

  • (AttributeDefinition)

    Attribute’s definition

Raises:



124
125
126
127
128
# File 'lib/neo4j/shared/attributes.rb', line 124

def attribute(name)
  fail Neo4j::DangerousAttributeError, %(an attribute method named "#{name}" would conflict with an existing method) if dangerous_attribute?(name)

  attribute!(name)
end

#attribute_namesArray<String>

Returns an Array of attribute names as Strings

Examples:

Get attribute names

Person.attribute_names

Returns:

  • (Array<String>)

    The attribute names



136
137
138
# File 'lib/neo4j/shared/attributes.rb', line 136

def attribute_names
  attributes.keys
end

#attributesActiveSupport::HashWithIndifferentAccess{String => Neo4j::Shared::AttributeDefinition}

Returns a Hash of AttributeDefinition instances

Examples:

Get attribute definitions

Person.attributes

Returns:

  • (ActiveSupport::HashWithIndifferentAccess{String => Neo4j::Shared::AttributeDefinition})

    The Hash of AttributeDefinition instances



147
148
149
# File 'lib/neo4j/shared/attributes.rb', line 147

def attributes
  @attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end

#dangerous_attribute?(name) ⇒ false, String

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.

Examples:

Testing a harmless attribute

Person.dangerous_attribute? :name #=> false

Testing a dangerous attribute

Person.dangerous_attribute? :nil #=> "nil?"

Parameters:

  • name

    Attribute name

Returns:

  • (false, String)

    False or the conflicting method name



167
168
169
170
171
172
173
# File 'lib/neo4j/shared/attributes.rb', line 167

def dangerous_attribute?(name)
  methods = instance_methods

  attribute_methods(name).detect do |method_name|
    !DEPRECATED_OBJECT_METHODS.include?(method_name.to_s) && methods.include?(method_name)
  end unless attribute_names.include? name.to_s
end

#inspectString

Returns the class name plus its attribute names

Examples:

Inspect the model’s definition.

Person.inspect

Returns:

  • (String)

    Human-readable presentation of the attributes



181
182
183
184
185
# File 'lib/neo4j/shared/attributes.rb', line 181

def inspect
  inspected_attributes = attribute_names.sort
  attributes_list = "(#{inspected_attributes.join(', ')})" unless inspected_attributes.empty?
  "#{name}#{attributes_list}"
end