Class: RequestHandler::FieldSetHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/request_handler/field_set_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(params:, allowed: {}, required: []) ⇒ FieldSetHandler

Returns a new instance of FieldSetHandler.

Raises:



6
7
8
9
10
11
12
13
14
# File 'lib/request_handler/field_set_handler.rb', line 6

def initialize(params:, allowed: {}, required: [])
  @params = params
  allowed.each_value do |option|
    raise InternalArgumentError, allowed: 'must be a Enum' unless option.is_a?(Dry::Types::Enum)
  end
  @allowed = allowed
  raise InternalArgumentError, allowed: 'must be an Array' unless required.is_a?(Array)
  @required = required
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/request_handler/field_set_handler.rb', line 16

def run
  fields = params['fields']
  raise_missing_fields_param unless fields

  field_set = fields.to_h.each_with_object({}) do |(type, values), memo|
    type = type.to_sym
    raise_invalid_field_option(type)
    memo[type] = parse_options(type, values)
  end
  check_required_field_set_types(field_set)
end