Method: ExplicitParameters::Parameters.accepts

Defined in:
lib/explicit_parameters/parameters.rb

.accepts(name, type = nil, options = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/explicit_parameters/parameters.rb', line 35

def accepts(name, type = nil, options = {}, &block)
  if block_given?
    subtype = define(name, &block)
    if type == Array
      type = Array[subtype]
    elsif type == nil
      type = subtype
    else
      raise ArgumentError, "`type` argument can only be `nil` or `Array` when a block is provided"
    end
  end
  attribute(name, type, options.slice(:default, :required))
  validations = options.except(:default)
  validations[:coercion] = true
  validates(name, validations)
end