Class: Broi::Input
- Inherits:
-
Dry::Struct::Value
- Object
- Dry::Struct::Value
- Broi::Input
- Defined in:
- lib/broi/input.rb
Defined Under Namespace
Modules: Types
Class Method Summary collapse
- .attribute(name, type = nil, **opts, &block) ⇒ Object
- .call(params = {}) ⇒ Object
- .validate(&block) ⇒ Object
- .validation ⇒ Object
Class Method Details
.attribute(name, type = nil, **opts, &block) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/broi/input.rb', line 16 def attribute(name, type = nil, **opts, &block) type ||= Types::Any type = type.optional.(omittable: true) type = type.default(opts[:default]) if opts.has_key?(:default) super(name, type, &block) end |
.call(params = {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/broi/input.rb', line 31 def call(params = {}) result = validation.(params) if result.success? Dry::Monads::Success.new new(result.output) else Dry::Monads::Failure.new result.errors end end |
.validate(&block) ⇒ Object
23 24 25 |
# File 'lib/broi/input.rb', line 23 def validate(&block) @validation = Dry::Validation::Schema(&block) end |
.validation ⇒ Object
27 28 29 |
# File 'lib/broi/input.rb', line 27 def validation @validation ||= Dry::Validation.Schema {} end |