Class: Effective::ModelReader
- Inherits:
-
Object
- Object
- Effective::ModelReader
- Defined in:
- app/models/effective/model_reader.rb
Constant Summary collapse
- DATATYPES =
[ :binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text, :uuid, :permitted_param ]
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
-
#initialize(&block) ⇒ ModelReader
constructor
A new instance of ModelReader.
-
#message(*args) ⇒ Object
For EffectiveLogging.
- #method_missing(m, *args, &block) ⇒ Object
- #read(&block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ ModelReader
Returns a new instance of ModelReader.
10 11 12 |
# File 'app/models/effective/model_reader.rb', line 10 def initialize(&block) @attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/effective/model_reader.rb', line 23 def method_missing(m, *args, &block) if m == :timestamps attributes[:created_at] = [:datetime] attributes[:updated_at] = [:datetime] return end # Not really an attribute, just a permitted param. # something permitted: true if args.first.kind_of?(Hash) && args.first.key?(:permitted) args.unshift(:permitted_param) end # Specifying permitted param attributes # invitation [:name, :email], permitted: true if args.first.kind_of?(Array) = args.find { |arg| arg.kind_of?(Hash) } || { permitted: true } args = [:permitted_param, .merge(permitted_attributes: args.first)] end unless DATATYPES.include?(args.first) raise "expected first argument to be a datatype. Try name :string" end attributes[m] = args end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'app/models/effective/model_reader.rb', line 8 def attributes @attributes end |
Instance Method Details
#message(*args) ⇒ Object
For EffectiveLogging. This is a protected keyword I think.
15 16 17 |
# File 'app/models/effective/model_reader.rb', line 15 def (*args) method_missing(:message, args) end |
#read(&block) ⇒ Object
19 20 21 |
# File 'app/models/effective/model_reader.rb', line 19 def read(&block) instance_exec(&block) end |