Module: SmartParams
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/smart_params.rb,
lib/smart_params/error.rb,
lib/smart_params/field.rb,
lib/smart_params/version.rb,
lib/smart_params/error/invalid_property_type.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"2.0.6"
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #as_json(options = nil) ⇒ Object
- #initialize(raw, safe: true) ⇒ Object
- #method_missing(name, *arguments, &block) ⇒ Object
- #payload ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/smart_params.rb', line 50 def method_missing(name, *arguments, &block) if payload.respond_to?(name) payload.public_send(name) else super end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
17 18 19 |
# File 'lib/smart_params.rb', line 17 def fields @fields end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
15 16 17 |
# File 'lib/smart_params.rb', line 15 def raw @raw end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
16 17 18 |
# File 'lib/smart_params.rb', line 16 def schema @schema end |
Instance Method Details
#as_json(options = nil) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/smart_params.rb', line 42 def as_json( = nil) if @exception.present? @exception.as_json() else structure.as_json() end end |
#initialize(raw, safe: true) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/smart_params.rb', line 19 def initialize(raw, safe: true) @safe = safe @raw = raw @schema = self.class.instance_variable_get(:@schema) @fields = [@schema, *unfold(@schema.subfields)] .sort_by(&:weight) .each { |field| field.claim(raw) } rescue SmartParams::Error::InvalidPropertyType => invalid_property_type_exception if safe? raise invalid_property_type_exception else @exception = invalid_property_type_exception end end |
#payload ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/smart_params.rb', line 34 def payload if @exception.present? @exception else RecursiveOpenStruct.new(structure) end end |