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



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

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

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



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

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

  validates_each(*attributes, **options) do |command, method_name, _attr_value|
    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