Class: Lite::Command::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, options) ⇒ Attribute

Returns a new instance of Attribute.



10
11
12
13
14
# File 'lib/lite/command/attribute.rb', line 10

def initialize(method_name, options)
  @method_name = method_name
  @options = options
  @errors = []
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/lite/command/attribute.rb', line 8

def errors
  @errors
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



8
9
10
# File 'lib/lite/command/attribute.rb', line 8

def method_name
  @method_name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/lite/command/attribute.rb', line 8

def options
  @options
end

Instance Method Details

#filled?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/lite/command/attribute.rb', line 20

def filled?
  Utils.call(command, options[:filled]) || false
end

#fromObject



16
17
18
# File 'lib/lite/command/attribute.rb', line 16

def from
  options[:from] || :context
end

#required?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/lite/command/attribute.rb', line 24

def required?
  Utils.call(command, options[:required]) || false
end

#typed?Boolean

Returns:

  • (Boolean)


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

def typed?
  options.key?(:types) && types.any?
end

#typesObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lite/command/attribute.rb', line 32

def types
  @types ||= begin
    t = Array(Utils.call(command, options[:types]))

    if filled?
      t.uniq - [NilClass]
    else
      t | [NilClass]
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lite/command/attribute.rb', line 53

def valid?
  errors.empty?
end

#validate!Object



44
45
46
47
48
49
50
51
# File 'lib/lite/command/attribute.rb', line 44

def validate!
  validate_respond_attribute!
  return unless errors.empty?

  validate_required_attribute!
  validate_attribute_type!
  validate_attribute_filled!
end

#valueObject



57
58
59
60
61
# File 'lib/lite/command/attribute.rb', line 57

def value
  return @value if defined?(@value)

  @value = command.send(from).public_send(method_name)
end