Class: TTY::Option::Parameters

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tty/option/parameters.rb

Overview

A collection to hold all parameters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParameters

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A parameters list



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tty/option/parameters.rb', line 57

def initialize
  @arguments = []
  @environments = []
  @keywords = []
  @options = []
  @list = []

  @registered_keys = Set.new
  @registered_shorts = Set.new
  @registered_longs = Set.new
end

Instance Attribute Details

#argumentsObject (readonly)

A list of arguments



30
31
32
# File 'lib/tty/option/parameters.rb', line 30

def arguments
  @arguments
end

#environmentsObject (readonly)

A list of environments



36
37
38
# File 'lib/tty/option/parameters.rb', line 36

def environments
  @environments
end

#keywordsObject (readonly)

A list of keywords



33
34
35
# File 'lib/tty/option/parameters.rb', line 33

def keywords
  @keywords
end

#listObject (readonly)

A list of all parameters



42
43
44
# File 'lib/tty/option/parameters.rb', line 42

def list
  @list
end

#optionsObject (readonly)

A list of options



39
40
41
# File 'lib/tty/option/parameters.rb', line 39

def options
  @options
end

Class Method Details

.define_param_query(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define a predicate method to check if a parameter is supported



23
24
25
26
27
# File 'lib/tty/option/parameters.rb', line 23

def self.define_param_query(name)
  define_method(:"#{name}?") do |param|
    public_send(:"#{name}s").map(&:key).include?(param)
  end
end

.define_query(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Define a query for parameter types



14
15
16
17
18
# File 'lib/tty/option/parameters.rb', line 14

def self.define_query(name)
  define_method(:"#{name}?") do
    !public_send(name).empty?
  end
end

Instance Method Details

#<<(parameter) ⇒ Object Also known as: add

Add parameter

Parameters:



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tty/option/parameters.rb', line 74

def <<(parameter)
  check_key_uniqueness!(parameter.key)

  if parameter.to_sym == :option
    check_short_option_uniqueness!(parameter.short_name)
    check_long_option_uniqueness!(parameter.long_name)
  end

  @list << parameter
  arr = instance_variable_get("@#{parameter.to_sym}s")
  arr.send :<<, parameter
  self
end

#delete(*keys) ⇒ Object

Delete a parameter from the list

Examples:

delete(:foo, :bar, :baz)

Parameters:

  • keys (Array<Symbol>)

    the keys to delete



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tty/option/parameters.rb', line 98

def delete(*keys)
  deleted = []
  @list.delete_if { |p| keys.include?(p.key) && (deleted << p) }
  deleted.each do |param|
    params_list = instance_variable_get("@#{param.to_sym}s")
    params_list.delete(param)
  end
  @registered_keys.subtract(keys)
  @registered_shorts.replace(@options.map(&:short))
  @registered_longs.replace(@options.map(&:long))
  deleted
end

#dupObject

Make a deep copy of the list of parameters



125
126
127
128
129
130
131
132
# File 'lib/tty/option/parameters.rb', line 125

def dup
  super.tap do |params|
    params.instance_variables.each do |var|
      dupped = DeepDup.deep_dup(params.instance_variable_get(var))
      params.instance_variable_set(var, dupped)
    end
  end
end

#each(&block) ⇒ Object

Enumerate all parameters



114
115
116
117
118
119
120
# File 'lib/tty/option/parameters.rb', line 114

def each(&block)
  if block_given?
    @list.each(&block)
  else
    to_enum(:each)
  end
end