Class: Lite::Command::AttributeValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/command/attribute_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ AttributeValidator

Returns a new instance of AttributeValidator.



9
10
11
# File 'lib/lite/command/attribute_validator.rb', line 9

def initialize(command)
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



7
8
9
# File 'lib/lite/command/attribute_validator.rb', line 7

def command
  @command
end

Instance Method Details

#attributesObject



13
14
15
16
17
18
# File 'lib/lite/command/attribute_validator.rb', line 13

def attributes
  @attributes ||=
    command.class.attributes.map do |_method_name, attribute|
      attribute.tap { |a| a.command = command }
    end
end

#errorsObject



20
21
22
23
24
25
26
27
# File 'lib/lite/command/attribute_validator.rb', line 20

def errors
  @errors ||= attributes.each_with_object({}) do |attribute, h|
    next if attribute.tap(&:validate!).valid?

    h[attribute.from] ||= []
    h[attribute.from] = h[attribute.from] | attribute.errors
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  attributes.empty? || errors.empty?
end