Class: NRSER::Labs::LOTS::OptConsumer

Inherits:
Consumer show all
Defined in:
lib/nrser/labs/lots/consumer.rb

Instance Method Summary collapse

Constructor Details

#initialize(name:, aliases: []) ⇒ OptConsumer

Returns a new instance of OptConsumer.



45
46
47
48
# File 'lib/nrser/labs/lots/consumer.rb', line 45

def initialize name:, aliases: []
  @name = name
  @aliases = aliases.dup
end

Instance Method Details

#can_consume?(tokens) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nrser/labs/lots/consumer.rb', line 58

def can_consume? tokens
  return 0 unless consumed.empty?

  first = tokens[0]

  return 1 if long_names.any? { |name|
    /\-\-#{ name }\=.*/ =~ first
  }

  if  tokens.length > 1 &&
      names.any? { |name|
        if name.length == 1
          first == "-#{ name }"
        else
          first == "--#{ name }"
        end
      }
    return 2
  end

  return 0
end

#long_namesObject



54
55
56
# File 'lib/nrser/labs/lots/consumer.rb', line 54

def long_names
  @long_names ||= @names.select { |name| name.length > 1 }
end

#namesObject



50
51
52
# File 'lib/nrser/labs/lots/consumer.rb', line 50

def names
  @names ||= [name, *aliases]
end

#satisfied?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/nrser/labs/lots/consumer.rb', line 81

def satisfied?
  if consumed.empty?
    type.test?( nil )
  else
    true
  end
end