Module: QuantumFields::NoSqlize
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/quantum_fields/no_sqlize.rb
Overview
Module to enable virtual methods in moduls, it creates necessary methods to simulate the behavior that any field can exist, getting or setting it is like a migrated column behaves.
Instance Method Summary collapse
-
#_assign_attribute(key, value) ⇒ Object
Overriding a ActiveRecord method that is used in ‘.create` and `.update` methods to assign a value into an attribute.
-
#method_missing(meth, *args, &block) ⇒ Object
Overriding ‘method_missing` does the magic of assigning an absent field into `.parameters` JSON as a key-value pair, or recovering it’s value if exists.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Used to map which methods can be called in the instance, it is based on the premisse that you can always assign a value to any field and recover only existing fields or the ones that are in the ‘.parameters` JSON.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Overriding ‘method_missing` does the magic of assigning an absent field into `.parameters` JSON as a key-value pair, or recovering it’s value if exists.
13 14 15 16 17 18 19 20 |
# File 'lib/quantum_fields/no_sqlize.rb', line 13 def method_missing(meth, *args, &block) method_name = meth.to_s super if method_name.ends_with?('!') bad_config! unless can_no_sqlize? no_sqlize!(method_name, *args, &block) rescue StandardError super end |
Instance Method Details
#_assign_attribute(key, value) ⇒ Object
Overriding a ActiveRecord method that is used in ‘.create` and `.update` methods to assign a value into an attribute.
34 35 36 37 38 |
# File 'lib/quantum_fields/no_sqlize.rb', line 34 def _assign_attribute(key, value) initialize_fields public_send("#{key}=", value) || try(fields_column).try(:except!, key.to_s) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Used to map which methods can be called in the instance, it is based on the premisse that you can always assign a value to any field and recover only existing fields or the ones that are in the ‘.parameters` JSON.
25 26 27 28 29 30 |
# File 'lib/quantum_fields/no_sqlize.rb', line 25 def respond_to_missing?(method_name, include_private = false) bad_config! unless can_no_sqlize? meth = method_name.to_s meth.ends_with?('=') || meth.ends_with?('?') || try(fields_column).try(:[], meth).present? || super end |