Module: ActiveRemote::Attributes::ClassMethods
- Defined in:
- lib/active_remote/attributes.rb
Instance Method Summary collapse
-
#attribute(name, options = {}) ⇒ Object
Defines an attribute.
-
#attribute!(name, options = {}) ⇒ Object
Defines an attribute without checking for conflicts.
-
#attribute_names ⇒ Object
Returns an Array of attribute names as Strings.
-
#attributes ⇒ Object
Returns a Hash of AttributeDefinition instances.
-
#dangerous_attribute?(name) ⇒ Boolean
Determine if a given attribute name is dangerous.
-
#inspect ⇒ Object
Returns the class name plus its attribute names.
Instance Method Details
#attribute(name, options = {}) ⇒ Object
Defines an attribute
For each attribute that is defined, a getter and setter will be added as an instance method to the model. An ActiveRemote::AttributeDefinition instance will be added to result of the attributes class method.
91 92 93 94 95 96 97 |
# File 'lib/active_remote/attributes.rb', line 91 def attribute(name, ={}) if dangerous_attribute_method_name = dangerous_attribute?(name) raise ::ActiveRemote::DangerousAttributeError, %{an attribute method named "#{dangerous_attribute_method_name}" would conflict with an existing method} else attribute!(name, ) end end |
#attribute!(name, options = {}) ⇒ Object
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.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_remote/attributes.rb', line 110 def attribute!(name, ={}) ::ActiveRemote::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 build_default_attributes_hash end end |
#attribute_names ⇒ Object
Returns an Array of attribute names as Strings
126 127 128 |
# File 'lib/active_remote/attributes.rb', line 126 def attribute_names attributes.keys end |
#attributes ⇒ Object
Returns a Hash of AttributeDefinition instances
135 136 137 |
# File 'lib/active_remote/attributes.rb', line 135 def attributes @attributes ||= ::ActiveSupport::HashWithIndifferentAccess.new end |
#dangerous_attribute?(name) ⇒ Boolean
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.
152 153 154 155 156 157 158 |
# File 'lib/active_remote/attributes.rb', line 152 def dangerous_attribute?(name) return false if attribute_names.include?(name.to_s) attribute_methods(name).detect do |method_name| allocate.respond_to?(method_name, true) end end |
#inspect ⇒ Object
Returns the class name plus its attribute names
165 166 167 168 169 |
# File 'lib/active_remote/attributes.rb', line 165 def inspect inspected_attributes = attribute_names.sort attributes_list = "(#{inspected_attributes.join(", ")})" unless inspected_attributes.empty? "#{name}#{attributes_list}" end |