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 =
"5.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) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/smart_params.rb', line 71

def method_missing(name, *arguments, &)
  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

#initialize(raw, safe: true, name: :default) ⇒ Object



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

def initialize(raw, safe: true, name: :default)
  @safe = safe
  @raw = raw
  @schema = self.class.instance_variable_get(:@schema)[name]

  @fields = [@schema, *unfold(@schema.subfields)].sort_by(&:weight).each { |field| field.claim(raw) }
rescue SmartParams::Error::InvalidPropertyType => invalid_property_exception
  raise invalid_property_exception if safe?

  @exception = invalid_property_exception
end

#inspectObject



33
34
35
# File 'lib/smart_params.rb', line 33

def inspect
  "#<#{self.class.name}:#{__id__} @fields=#{@fields.inspect} @raw=#{@raw.inspect}>"
end

#payloadObject



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

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

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/smart_params.rb', line 67

def respond_to_missing?(name, include_private = false)
  payload.respond_to?(name) || super
end

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



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

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