Module: Neo4j::Shared::Attributes::ClassMethods
- Defined in:
- lib/neo4j/shared/attributes.rb
Instance Method Summary collapse
-
#attribute(name) ⇒ AttributeDefinition
Defines an attribute.
-
#attribute_names ⇒ Array<String>
Returns an Array of attribute names as Strings.
-
#attributes ⇒ ActiveSupport::HashWithIndifferentAccess{String => Neo4j::Shared::AttributeDefinition}
Returns a Hash of AttributeDefinition instances.
-
#dangerous_attribute?(name) ⇒ false, String
Determine if a given attribute name is dangerous.
-
#inspect ⇒ String
Returns the class name plus its attribute names.
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.
128 129 130 131 132 133 134 |
# File 'lib/neo4j/shared/attributes.rb', line 128 def attribute(name) if dangerous_attribute?(name) fail Neo4j::DangerousAttributeError, %(an attribute method named "#{name}" would conflict with an existing method) else attribute!(name) end end |
#attribute_names ⇒ Array<String>
Returns an Array of attribute names as Strings
142 143 144 |
# File 'lib/neo4j/shared/attributes.rb', line 142 def attribute_names attributes.keys end |
#attributes ⇒ ActiveSupport::HashWithIndifferentAccess{String => Neo4j::Shared::AttributeDefinition}
Returns a Hash of AttributeDefinition instances
153 154 155 |
# File 'lib/neo4j/shared/attributes.rb', line 153 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.
173 174 175 176 177 |
# File 'lib/neo4j/shared/attributes.rb', line 173 def dangerous_attribute?(name) attribute_methods(name).detect do |method_name| !DEPRECATED_OBJECT_METHODS.include?(method_name.to_s) && allocate.respond_to?(method_name, true) end unless attribute_names.include? name.to_s end |
#inspect ⇒ String
Returns the class name plus its attribute names
185 186 187 188 189 |
# File 'lib/neo4j/shared/attributes.rb', line 185 def inspect inspected_attributes = attribute_names.sort attributes_list = "(#{inspected_attributes.join(', ')})" unless inspected_attributes.empty? "#{name}#{attributes_list}" end |