Module: Lite::Command::Internals::Attributes::ClassMethods

Defined in:
lib/lite/command/internals/attributes.rb

Instance Method Summary collapse

Instance Method Details

#optional(*attributes, from: :context, **_options) ⇒ Object



29
30
31
# File 'lib/lite/command/internals/attributes.rb', line 29

def optional(*attributes, from: :context, **_options)
  delegate(*attributes, from:)
end

#required(*attributes, from: :context, **options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lite/command/internals/attributes.rb', line 14

def required(*attributes, from: :context, **options)
  delegate(*attributes, from:)

  validates_each(*attributes, **options) do |command, method_name, _attr_value|
    next unless Utils.evaluate(command, options)
    next if command.errors.added?(from, :undefined) || command.errors.added?(method_name, :required)

    if !command.respond_to?(from, true)
      command.errors.add(from, :undefined, message: "is an undefined argument")
    elsif !command.send(from).respond_to?(method_name, true)
      command.errors.add(method_name, :required, message: "is a required argument")
    end
  end
end