Module: ActiveAttr::Attributes::ClassMethods

Defined in:
lib/active_attr/attributes.rb

Overview

Since:

  • 0.2.0

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ 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 ActiveAttr::AttributeDefinition instance will be added to result of the attributes class method.

Examples:

Define an attribute.

attribute :name

Parameters:

  • name (Symbol, String, #to_sym)

    attribute name

  • options (Hash{Symbol => Object}) (defaults to: {})

    attribute options

Returns:

Raises:

Since:

  • 0.2.0



228
229
230
231
232
233
234
# File 'lib/active_attr/attributes.rb', line 228

def attribute(name, options={})
  if dangerous_attribute_method_name = dangerous_attribute?(name)
    raise DangerousAttributeError, %{an attribute method named "#{dangerous_attribute_method_name}" would conflict with an existing method}
  else
    attribute! name, options
  end
end

#attribute!(name, options = {}) ⇒ AttributeDefinition

Defines an attribute without checking for conflicts

Allows you to define an attribute whose methods will conflict with an existing method. For example, Ruby’s Timeout library adds a timeout method to Object. Attempting to define a timeout attribute using .attribute will raise a DangerousAttributeError, but .attribute! will not.

Examples:

Define a dangerous attribute.

attribute! :timeout

Parameters:

  • name (Symbol, String, #to_sym)

    attribute name

  • options (Hash{Symbol => Object}) (defaults to: {})

    attribute options

Returns:

Since:

  • 0.6.0



252
253
254
255
256
257
258
259
260
261
# File 'lib/active_attr/attributes.rb', line 252

def attribute!(name, options={})
  AttributeDefinition.new(name, options).tap do |attribute_definition|
    attribute_name = attribute_definition.name.to_s
    # Force active model to generate attribute methods
    remove_instance_variable("@attribute_methods_generated") if instance_variable_defined?("@attribute_methods_generated")
    define_attribute_methods([attribute_definition.name]) unless attribute_names.include? attribute_name
    remove_instance_variable("@attribute_names") if instance_variable_defined?("@attribute_names")
    attributes[attribute_name] = attribute_definition
  end
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

Since:

  • 0.5.0



271
272
273
# File 'lib/active_attr/attributes.rb', line 271

def attribute_names
  @attribute_names ||= attributes.keys
end

#attributesActiveSupport::HashWithIndifferentAccess{String => ActiveAttr::AttributeDefinition}

Returns a Hash of AttributeDefinition instances

Examples:

Get attribute definitions

Person.attributes

Returns:

Since:

  • 0.2.0



284
285
286
# File 'lib/active_attr/attributes.rb', line 284

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

Since:

  • 0.6.0



306
307
308
309
310
# File 'lib/active_attr/attributes.rb', line 306

def dangerous_attribute?(name)
  attribute_methods(name).detect do |method_name|
    allocate.respond_to?(method_name, true)
  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

Since:

  • 0.2.0



320
321
322
323
324
# File 'lib/active_attr/attributes.rb', line 320

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