Class: EasyParams::Base
- Inherits:
-
Dry::Struct
- Object
- Dry::Struct
- EasyParams::Base
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/easy_params/base.rb
Overview
Implements validations logic and nesting structures
Class Method Summary collapse
- .array(param_name, of:, normalize: nil, **validations, &block) ⇒ Object
- .each(param_name, normalize: nil, **validations, &block) ⇒ Object
- .has(param_name, normalize: nil, **validations, &block) ⇒ Object
- .name ⇒ Object
- .param(method_name, type_name, of: nil, default: nil, **validations, &block) ⇒ Object
Class Method Details
.array(param_name, of:, normalize: nil, **validations, &block) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/easy_params/base.rb', line 43 def self.array(param_name, of:, normalize: 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.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, **validations, &block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/easy_params/base.rb', line 29 def self.each(param_name, normalize: nil, **validations, &block) validates param_name, **validations if validations.any? type = EasyParams::Types::Each 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, **validations, &block) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/easy_params/base.rb', line 36 def self.has(param_name, normalize: nil, **validations, &block) validates param_name, **validations if validations.any? type = EasyParams::Types::Struct type = type.constructor { |value| value == Dry::Types::Undefined ? value : normalize.call(value) } if normalize public_send(:attribute, param_name, type, &block) end |
.name ⇒ Object
10 11 12 |
# File 'lib/easy_params/base.rb', line 10 def self.name 'EasyParams::Base' end |
.param(method_name, type_name, of: nil, default: nil, **validations, &block) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/easy_params/base.rb', line 51 def self.param(method_name, type_name, of: nil, default: nil, **validations, &block) type = EasyParams::Types.const_get(type_name.to_s.camelcase) type = type.default(default) if default type = type.of(EasyParams::Types.const_get(of.to_s.camelcase)) if of && type_name != :each validates method_name, **validations if validations.any? public_send(:attribute, method_name, type, &block) end |