Method: ActiveAttr::Attributes::ClassMethods#attribute!

Defined in:
lib/active_attr/attributes.rb

#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