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(command, method_name, options) ⇒ Attribute

Returns a new instance of Attribute.



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

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

Instance Attribute Details

#commandObject (readonly)

TODO: allow procs



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

def command
  @command
end

#errorsObject (readonly)

TODO: allow procs



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

def errors
  @errors
end

#method_nameObject (readonly)

TODO: allow procs



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

def method_name
  @method_name
end

#optionsObject (readonly)

TODO: allow procs



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

def options
  @options
end

Instance Method Details

#filled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/lite/command/attribute.rb', line 22

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

#fromObject



18
19
20
# File 'lib/lite/command/attribute.rb', line 18

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

#required?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lite/command/attribute.rb', line 26

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

#typed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/lite/command/attribute.rb', line 30

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

#typesObject



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

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

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

#valid?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/lite/command/attribute.rb', line 55

def valid?
  errors.empty?
end

#validate!Object



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

def validate!
  validate_respond_attribute!
  return unless errors.empty?

  validate_required_attribute!
  validate_attribute_type!
  validate_attribute_filled!
end

#valueObject



59
60
61
62
63
# File 'lib/lite/command/attribute.rb', line 59

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

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