Module: Gate::Command::ClassMethods

Defined in:
lib/gate/command.rb

Instance Method Summary collapse

Instance Method Details

#schema(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gate/command.rb', line 36

def schema(&block)
  if block_given?
    raise SchemaAlreadyRegistered if instance_variable_defined?(:@schema)
    @schema = Dry::Validation.Form(&block)
    @schema.rules.keys.each do |name|
      define_method(name) do
        result[name]
      end
    end
  else
    raise SchemaNotDefined unless @schema
    @schema
  end
end

#with(input) ⇒ Object

Raises:



51
52
53
54
55
# File 'lib/gate/command.rb', line 51

def with(input)
  result = schema.(input)
  raise InvalidCommand, result.messages(full: true) if result.failure?
  new result.output
end