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 =
"3.0.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



60
61
62
63
64
65
66
# File 'lib/smart_params.rb', line 60

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

#initialize(raw, safe: true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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
  raise invalid_property_type_exception if safe?

  @exception = invalid_property_type_exception
end

#payloadObject



30
31
32
33
34
35
36
# File 'lib/smart_params.rb', line 30

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

#to_hash(options = nil) ⇒ Object Also known as: as_json



38
39
40
41
42
43
44
# File 'lib/smart_params.rb', line 38

def to_hash(options = nil)
  if @exception.present?
    @exception.as_json(options)
  else
    structure.as_json(options) || {}
  end
end