Module: Persisted::ClassMethods
- Defined in:
- lib/persisted.rb
Instance Attribute Summary collapse
-
#persisted_record_class ⇒ Object
Returns the value of attribute persisted_record_class.
Instance Method Summary collapse
- #method_missing(method_name, *args) ⇒ Object
-
#persist_with(model_name, options = {}) ⇒ Object
Instructs the persistence layer which ORM-backed class to use to persist the model.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/persisted.rb', line 59 def method_missing( method_name, *args ) if respond_to_missing? method_name persisted_record_class.send method_name, *args else super end end |
Instance Attribute Details
#persisted_record_class ⇒ Object
Returns the value of attribute persisted_record_class.
31 32 33 |
# File 'lib/persisted.rb', line 31 def persisted_record_class @persisted_record_class end |
Instance Method Details
#persist_with(model_name, options = {}) ⇒ Object
Instructs the persistence layer which ORM-backed class to use to persist the model.
Usage:
persist_with :user, attributes: %w{username password}
Configuration options: attributes: An array of shared attributes between models used for DB
interactions. Basically, use this to find the appropriate record.
Defaults to ['id']
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/persisted.rb', line 43 def persist_with( model_name, = {} ) self.persisted_record_class = model_name.to_s.camelize.constantize if && .is_a?( Hash ) .symbolize_keys! if [:attributes].is_a?( Array ) [:attributes] ||= ['id'] @shared_attributes = [:attributes] end else raise ArgumentError, 'An array of shared attributes must be supplied as the :attributes option of the configuration hash' end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
67 68 69 |
# File 'lib/persisted.rb', line 67 def respond_to_missing?( method_name, include_private = false ) persisted_record_class.respond_to? method_name end |