Module: ActiveAttr::Attributes::ClassMethods
- Defined in:
- lib/active_attr/attributes.rb
Overview
Instance Method Summary collapse
-
#attribute(name, options = {}) ⇒ AttributeDefinition
Defines an attribute.
-
#attribute!(name, options = {}) ⇒ AttributeDefinition
Defines an attribute without checking for conflicts.
-
#attribute_names ⇒ Array<String>
Returns an Array of attribute names as Strings.
-
#attributes ⇒ ActiveSupport::HashWithIndifferentAccess{String => ActiveAttr::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, 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.
175 176 177 178 179 180 181 |
# File 'lib/active_attr/attributes.rb', line 175 def attribute(name, ={}) 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, 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.
199 200 201 202 203 204 205 206 207 |
# File 'lib/active_attr/attributes.rb', line 199 def attribute!(name, ={}) AttributeDefinition.new(name, ).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 attributes[attribute_name] = attribute_definition end end |
#attribute_names ⇒ Array<String>
Returns an Array of attribute names as Strings
217 218 219 |
# File 'lib/active_attr/attributes.rb', line 217 def attribute_names attributes.keys end |
#attributes ⇒ ActiveSupport::HashWithIndifferentAccess{String => ActiveAttr::AttributeDefinition}
Returns a Hash of AttributeDefinition instances
230 231 232 |
# File 'lib/active_attr/attributes.rb', line 230 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.
252 253 254 255 256 |
# File 'lib/active_attr/attributes.rb', line 252 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
266 267 268 269 270 |
# File 'lib/active_attr/attributes.rb', line 266 def inspect inspected_attributes = attribute_names.sort attributes_list = "(#{inspected_attributes.join(", ")})" unless inspected_attributes.empty? "#{name}#{attributes_list}" end |