Class: Broi::Input

Inherits:
Dry::Struct::Value
  • Object
show all
Defined in:
lib/broi/input.rb,
lib/broi/input/soft.rb,
lib/broi/input/utils.rb,
lib/broi/input/value.rb,
lib/broi/input/failure.rb,
lib/broi/input/success.rb,
lib/broi/input/invalid_error.rb,
lib/broi/input/invalid_value.rb

Defined Under Namespace

Modules: Soft, SoftInput, Types, Utils Classes: Failure, InvalidValue, Success, Value

Constant Summary collapse

Invalid =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, **opts, &block) ⇒ Object



28
29
30
31
32
# File 'lib/broi/input.rb', line 28

def attribute(name, **opts, &block)
  type = Types::Any.optional.meta(omittable: true)
  type = type.default(opts[:default]) if opts.has_key?(:default)
  super(name, type, &block)
end

.call(params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/broi/input.rb', line 42

def call(params = {})
  result = validation.(params)
  input = Soft.(self, result.output, result.errors)
  if result.success?
    Success.new(input)
  else
    Failure.new(input, Errors.new(result.errors))
  end
end

.validate(&block) ⇒ Object



34
35
36
# File 'lib/broi/input.rb', line 34

def validate(&block)
  @validation = Dry::Validation::Schema(&block)
end

.validationObject



38
39
40
# File 'lib/broi/input.rb', line 38

def validation
  @validation ||= Dry::Validation.Schema {}
end

Instance Method Details

#strict?Boolean



22
23
24
# File 'lib/broi/input.rb', line 22

def strict?
  true
end