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.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/quantum_fields/no_sqlize.rb', line 12 def method_missing(meth, *args, &block) if can_no_sqlize? no_sqlize!(meth, *args, &block) else raise NotImplementedError, "#{model.table_name} should have a `#{fields_column}` JSON column" end 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.
35 36 37 38 39 |
# File 'lib/quantum_fields/no_sqlize.rb', line 35 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.
26 27 28 29 30 31 |
# File 'lib/quantum_fields/no_sqlize.rb', line 26 def respond_to_missing?(method_name, include_private = false) meth = method_name.to_s can_no_sqlize? && meth.ends_with?('=') || meth.ends_with?('changed?') || try(fields_column).try(:[], meth).present? || super end |