Class: ExplicitParameters::Parameters

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Enumerable
Defined in:
lib/explicit_parameters/parameters.rb

Defined Under Namespace

Classes: CoercionValidator, RequiredValidator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Parameters

Returns a new instance of Parameters.



67
68
69
70
71
# File 'lib/explicit_parameters/parameters.rb', line 67

def initialize(attributes = {})
  attributes = attributes.to_unsafe_h if IS_RAILS5 && attributes.respond_to?(:to_unsafe_h)
  @original_attributes = attributes.stringify_keys
  super(attributes)
end

Class Method Details

.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

.define(name = nil, &block) ⇒ Object



27
28
29
# File 'lib/explicit_parameters/parameters.rb', line 27

def define(name = nil, &block)
  name_class(Class.new(self, &block), name)
end

.optional_attributesObject



52
53
54
# File 'lib/explicit_parameters/parameters.rb', line 52

def optional_attributes
  @optional_attributes ||= []
end

.parse!(params) ⇒ Object



23
24
25
# File 'lib/explicit_parameters/parameters.rb', line 23

def parse!(params)
  new(params).validate!
end

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



31
32
33
# File 'lib/explicit_parameters/parameters.rb', line 31

def requires(name, type = nil, options = {}, &block)
  accepts(name, type, options.merge(required: true), &block)
end

Instance Method Details

#to_hashObject



94
95
96
# File 'lib/explicit_parameters/parameters.rb', line 94

def to_hash
  super.except(*missing_attributes)
end

#validate!Object

Raises:



73
74
75
76
# File 'lib/explicit_parameters/parameters.rb', line 73

def validate!
  raise InvalidParameters.new({errors: errors}.to_json) unless valid?
  self
end

#validate_attribute_coercion!(attribute_name, value) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/explicit_parameters/parameters.rb', line 86

def validate_attribute_coercion!(attribute_name, value)
  return unless @original_attributes.key?(attribute_name.to_s)
  attribute = attribute_set[attribute_name]
  return if value.nil? && !attribute.required?
  return if attribute.value_coerced?(value)
  errors.add attribute_name, "#{@original_attributes[attribute_name].inspect} is not a valid #{attribute.type.name.demodulize}"
end

#validate_attribute_provided!(attribute_name, value) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/explicit_parameters/parameters.rb', line 78

def validate_attribute_provided!(attribute_name, value)
  if !@original_attributes.key?(attribute_name.to_s)
    errors.add attribute_name, 'is required'
  elsif attribute_set[attribute_name].type.primitive == Array && value == [].freeze
    errors.add attribute_name, 'is required'
  end
end