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

RECURSIVE_TREE =
->(accumulated, key) {accumulated[key] = Hash.new(&RECURSIVE_TREE)}
VERSION =
"2.0.4"

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



52
53
54
55
56
57
58
# File 'lib/smart_params.rb', line 52

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.



19
20
21
# File 'lib/smart_params.rb', line 19

def fields
  @fields
end

#rawObject (readonly)

Returns the value of attribute raw.



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

def raw
  @raw
end

#schemaObject (readonly)

Returns the value of attribute schema.



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

def schema
  @schema
end

Instance Method Details

#as_jsonObject



44
45
46
47
48
49
50
# File 'lib/smart_params.rb', line 44

def as_json
  if @exception.present?
    @exception.as_json
  else
    structure.deep_stringify_keys
  end
end

#initialize(raw, safe: true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/smart_params.rb', line 21

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



36
37
38
39
40
41
42
# File 'lib/smart_params.rb', line 36

def payload
  if @exception.present?
    @exception
  else
    RecursiveOpenStruct.new(structure)
  end
end