Module: ActiveEntity::AMAttributeMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- AttributeMethods, ActiveEntity::AttributeMethods::Dirty
- Defined in:
- lib/active_entity/attribute_methods.rb
Defined Under Namespace
Modules: AttrNames, ClassMethods
Constant Summary collapse
- NAME_COMPILABLE_REGEXP =
/\A[a-zA-Z_]\w*[!?=]?\z/- CALL_COMPILABLE_REGEXP =
/\A[a-zA-Z_]\w*[!?]?\z/
Instance Method Summary collapse
-
#attribute_missing(match, *args, &block) ⇒ Object
attribute_missingis likemethod_missing, but for attributes. -
#method_missing(method, *args, &block) ⇒ Object
Allows access to the object attributes, which are held in the hash returned by
attributes, as though they were first-class methods. - #respond_to?(method, include_private_methods = false) ⇒ Boolean
-
#respond_to_without_attributes? ⇒ Object
A
Personinstance with anameattribute can askperson.respond_to?(:name),person.respond_to?(:name=), andperson.respond_to?(:name?)which will all returntrue.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Allows access to the object attributes, which are held in the hash returned by attributes, as though they were first-class methods. So a Person class with a name attribute can for example use Person#name and Person#name= and never directly use the attributes hash – except for multiple assignments with ActiveRecord::Base#attributes=.
It’s also possible to instantiate related objects, so a Client class belonging to the clients table with a master_id foreign key can instantiate master through Client#master.
407 408 409 410 411 412 413 414 |
# File 'lib/active_entity/attribute_methods.rb', line 407 def method_missing(method, *args, &block) if respond_to_without_attributes?(method, true) super else match = matched_attribute_method(method.to_s) match ? attribute_missing(match, *args, &block) : super end end |
Instance Method Details
#attribute_missing(match, *args, &block) ⇒ Object
attribute_missing is like method_missing, but for attributes. When method_missing is called we check to see if there is a matching attribute method. If so, we tell attribute_missing to dispatch the attribute. This method can be overloaded to customize the behavior.
421 422 423 |
# File 'lib/active_entity/attribute_methods.rb', line 421 def attribute_missing(match, *args, &block) __send__(match.target, match.attr_name, *args, &block) end |
#respond_to?(method, include_private_methods = false) ⇒ Boolean
429 430 431 432 433 434 435 436 437 438 439 |
# File 'lib/active_entity/attribute_methods.rb', line 429 def respond_to?(method, include_private_methods = false) if super true elsif !include_private_methods && super(method, true) # If we're here then we haven't found among non-private methods # but found among all methods. Which means that the given method is private. false else !matched_attribute_method(method.to_s).nil? end end |
#respond_to_without_attributes? ⇒ Object
A Person instance with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true.
428 |
# File 'lib/active_entity/attribute_methods.rb', line 428 alias :respond_to_without_attributes? :respond_to? |