module SmartParams

Constants

VERSION

Attributes

fields[R]
raw[R]
schema[R]

Public Class Methods

new(raw, safe: true) click to toggle source
# 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

Public Instance Methods

as_json(options = nil)
Alias for: to_hash
method_missing(name, *arguments, &block) click to toggle source
Calls superclass method
# 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
payload() click to toggle source
# File lib/smart_params.rb, line 30
def payload
  if @exception.present?
    @exception
  else
    RecursiveOpenStruct.new(structure)
  end
end
to_hash(options = nil) click to toggle source
# 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
Also aliased as: as_json

Private Instance Methods

safe?() click to toggle source
# File lib/smart_params.rb, line 87
        def safe?
  @safe
end
structure() click to toggle source

This function basically takes a list of fields and reduces them into a tree of values

# File lib/smart_params.rb, line 69
        def structure
  fields
    .reject(&:removable?)
    .map(&:to_hash)
    .reduce(&:deep_merge)
end
unfold(subfields) click to toggle source

This funcion takes a nested field tree and turns it into a list of fields

# File lib/smart_params.rb, line 77
        def unfold(subfields)
  subfields.to_a.reduce([]) do |list, field|
    if field.deep?
      [*list, field, *unfold(field.subfields)]
    else
      [*list, field]
    end
  end.flatten
end