Class: EasyParams::Base

Inherits:
Dry::Struct
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/easy_params/base.rb

Overview

Implements validations logic and nesting structures

Class Method Summary collapse

Class Method Details

.array(param_name, of:, normalize: nil, optional: nil, **validations, &block) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/easy_params/base.rb', line 47

def self.array(param_name, of:, normalize: nil, optional: nil, **validations, &block)
  validates param_name, **validations if validations.any?
  of_type = EasyParams::Types.const_get(of.to_s.camelcase)
  type = EasyParams::Types::Array
  type = type.meta(omittable: true) if optional
  type = type.constructor { |value| value == Dry::Types::Undefined ? value : normalize.call(value) } if normalize
  public_send(:attribute, param_name, type.of(of_type), &block)
end

.each(param_name, normalize: nil, optional: nil, **validations, &block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/easy_params/base.rb', line 31

def self.each(param_name, normalize: nil, optional: nil, **validations, &block)
  validates param_name, **validations if validations.any?
  type = EasyParams::Types::Array.of(EasyParams::Types::Struct)
  type = type.meta(omittable: true) if optional
  type = type.constructor { |value| value == Dry::Types::Undefined ? value : normalize.call(value) } if normalize
  public_send(:attribute, param_name, type, &block)
end

.has(param_name, normalize: nil, optional: nil, **validations, &block) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/easy_params/base.rb', line 39

def self.has(param_name, normalize: nil, optional: nil, **validations, &block)
  validates param_name, **validations if validations.any?
  type = EasyParams::Types::Struct
  type = type.meta(omittable: true) if optional
  type = type.constructor { |value| value == Dry::Types::Undefined ? value : normalize.call(value) } if normalize
  public_send(:attribute, param_name, type, &block)
end

.nameObject



10
11
12
# File 'lib/easy_params/base.rb', line 10

def self.name
  'EasyParams::Base'
end