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

Classes: Error, Field

Constant Summary collapse

VERSION =
"2.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/smart_params.rb', line 62

def method_missing(name, *arguments, &block)
  if payload.respond_to?(name)
    payload.public_send(name)
  else
    super
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



17
18
19
# File 'lib/smart_params.rb', line 17

def fields
  @fields
end

#rawObject (readonly)

Returns the value of attribute raw.



15
16
17
# File 'lib/smart_params.rb', line 15

def raw
  @raw
end

#schemaObject (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(options = nil)
  if @exception.present?
    @exception.as_json(options)
  else
    structure.as_json(options)
  end
end

#dig(*keys) ⇒ Object



58
59
60
# File 'lib/smart_params.rb', line 58

def dig(*keys)
  as_json.dig(*keys)
end

#fetch(key, default = KeyError) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/smart_params.rb', line 50

def fetch(key, default = KeyError)
  if default == KeyError
    as_json.fetch(key, default)
  else
    as_json.fetch(key)
  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

#payloadObject



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