Class: TTY::Option::Parameter

Inherits:
Object
  • Object
show all
Includes:
Comparable, DSL::Arity, DSL::Conversion
Defined in:
lib/tty/option/parameter.rb,
lib/tty/option/parameter/option.rb,
lib/tty/option/parameter/keyword.rb,
lib/tty/option/parameter/argument.rb,
lib/tty/option/parameter/environment.rb

Direct Known Subclasses

Argument, Environment, Keyword, Option

Defined Under Namespace

Classes: Argument, Environment, Keyword, Option

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::Conversion

#list_of, #map_of

Methods included from DSL::Arity

#at_least, #one, #one_or_more, #two, #two_or_more, #zero_or_more

Constructor Details

#initialize(key, **settings, &block) ⇒ Parameter

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.

Create a parameter

Parameters:

  • key (Symbol)

    the key to register this param under


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tty/option/parameter.rb', line 32

def initialize(key, **settings, &block)
  @key = key
  @settings = {}
  settings.each do |key, val|
    case key.to_sym
    when :arity
      val = check_arity(val)
    when :default
      val = check_default(val)
    when :optional
      key, val = :required, check_required(!val)
    when :permit
      val = check_permitted(val)
    when :validate
      val = check_validation(val)
    end
    @settings[key.to_sym] = val
  end

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#keyObject (readonly)

The key under which this parameter is registered


24
25
26
# File 'lib/tty/option/parameter.rb', line 24

def key
  @key
end

Class Method Details

.create(key, **settings, &block) ⇒ Object

A parameter factory


17
18
19
# File 'lib/tty/option/parameter.rb', line 17

def self.create(key, **settings, &block)
  new(key, **settings, &block)
end

Instance Method Details

#<=>(other) ⇒ Object

Compare this parameter name with the other


197
198
199
# File 'lib/tty/option/parameter.rb', line 197

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Object

Compare parameters for equality based on type and name


204
205
206
207
# File 'lib/tty/option/parameter.rb', line 204

def ==(other)
  return false unless instance_of?(other.class)
  name == other.name && to_h == other.to_h
end

#arity(value = (not_set = true)) ⇒ Object


54
55
56
57
58
59
60
# File 'lib/tty/option/parameter.rb', line 54

def arity(value = (not_set = true))
  if not_set
    @settings.fetch(:arity) { default_arity }
  else
    @settings[:arity] = check_arity(value)
  end
end

#convert(value = (not_set = true)) ⇒ Object


82
83
84
85
86
87
88
# File 'lib/tty/option/parameter.rb', line 82

def convert(value = (not_set = true))
  if not_set
    @settings[:convert]
  else
    @settings[:convert] = value
  end
end

#convert?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'lib/tty/option/parameter.rb', line 90

def convert?
  @settings.key?(:convert) && !@settings[:convert].nil?
end

#default(value = (not_set = true)) ⇒ Object Also known as: defaults


94
95
96
97
98
99
100
# File 'lib/tty/option/parameter.rb', line 94

def default(value = (not_set = true))
  if not_set
    @settings[:default]
  else
    @settings[:default] = check_default(value)
  end
end

#default?Boolean

Returns:

  • (Boolean)

103
104
105
# File 'lib/tty/option/parameter.rb', line 103

def default?
  @settings.key?(:default) && !@settings[:default].nil?
end

#default_arityObject


62
63
64
# File 'lib/tty/option/parameter.rb', line 62

def default_arity
  1
end

#default_nameObject


174
175
176
# File 'lib/tty/option/parameter.rb', line 174

def default_name
  key.to_s.tr("_", "-")
end

#desc(value = (not_set = true)) ⇒ Object


107
108
109
110
111
112
113
# File 'lib/tty/option/parameter.rb', line 107

def desc(value = (not_set = true))
  if not_set
    @settings[:desc]
  else
    @settings[:desc] = value
  end
end

#desc?Boolean

Returns:

  • (Boolean)

115
116
117
# File 'lib/tty/option/parameter.rb', line 115

def desc?
  @settings.key?(:desc) && !@settings[:desc].nil?
end

#display?Boolean

Returns:

  • (Boolean)

150
151
152
# File 'lib/tty/option/parameter.rb', line 150

def display?
  desc? && !hidden?
end

#dupObject

Make a duplicate of this parameter


236
237
238
239
240
241
# File 'lib/tty/option/parameter.rb', line 236

def dup
  super.tap do |param|
    param.instance_variable_set(:@key, DeepDup.deep_dup(@key))
    param.instance_variable_set(:@settings, DeepDup.deep_dup(@settings))
  end
end

#eql?(other) ⇒ Boolean

Compare parameters for equality based on type and name

Returns:

  • (Boolean)

212
213
214
215
# File 'lib/tty/option/parameter.rb', line 212

def eql?(other)
  return false unless instance_of?(other.class)
  name.eql?(other.name) && to_h.eql?(other.to_h)
end

#hiddenObject


142
143
144
# File 'lib/tty/option/parameter.rb', line 142

def hidden
  @settings[:hidden] = true
end

#hidden?Boolean

Returns:

  • (Boolean)

146
147
148
# File 'lib/tty/option/parameter.rb', line 146

def hidden?
  @settings.fetch(:hidden) { false }
end

#min_arityObject

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.

Determine minimum boundary for arity parameter


69
70
71
# File 'lib/tty/option/parameter.rb', line 69

def min_arity
  arity < 0 ? arity.abs - 1 : arity
end

#multi_argument?Boolean

Check if this options is multi argument

Returns:

  • (Boolean)

122
123
124
# File 'lib/tty/option/parameter.rb', line 122

def multi_argument?
  !convert.to_s.match(/list|array|map|hash|\w+s/).nil?
end

#multiple?Boolean

Check if multiple occurrences are allowed

Returns:

  • (Boolean)

78
79
80
# File 'lib/tty/option/parameter.rb', line 78

def multiple?
  arity < 0 || 1 < arity.abs
end

#name(value = (not_set = true)) ⇒ Object


166
167
168
169
170
171
172
# File 'lib/tty/option/parameter.rb', line 166

def name(value = (not_set = true))
  if not_set
    @settings.fetch(:name) { default_name }
  else
    @settings[:name] = value
  end
end

#optionalObject


126
127
128
# File 'lib/tty/option/parameter.rb', line 126

def optional
  @settings[:required] = false
end

#optional?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'lib/tty/option/parameter.rb', line 130

def optional?
  !required?
end

#permit(value = (not_set = true)) ⇒ Object


154
155
156
157
158
159
160
# File 'lib/tty/option/parameter.rb', line 154

def permit(value = (not_set = true))
  if not_set
    @settings[:permit]
  else
    @settings[:permit] = check_permitted(value)
  end
end

#permit?Boolean

Returns:

  • (Boolean)

162
163
164
# File 'lib/tty/option/parameter.rb', line 162

def permit?
  @settings.key?(:permit) && !@settings[:permit].nil?
end

#requiredObject


134
135
136
# File 'lib/tty/option/parameter.rb', line 134

def required
  @settings[:required] = check_required(true)
end

#required?Boolean

Returns:

  • (Boolean)

138
139
140
# File 'lib/tty/option/parameter.rb', line 138

def required?
  @settings.fetch(:required) { false }
end

#to_h(&block) ⇒ Hash

Return a hash of this parameter settings

Returns:

  • (Hash)

    the names and values of this parameter


222
223
224
225
226
227
228
229
230
231
# File 'lib/tty/option/parameter.rb', line 222

def to_h(&block)
  if block_given?
    @settings.each_with_object({}) do |(key, val), acc|
      k, v = *block.(key, val)
      acc[k] = v
    end
  else
    DeepDup.deep_dup(@settings)
  end
end

#to_symObject


190
191
192
# File 'lib/tty/option/parameter.rb', line 190

def to_sym
  self.class.name.to_s.split(/::/).last.downcase.to_sym
end

#validate(value = (not_set = true)) ⇒ Object


178
179
180
181
182
183
184
# File 'lib/tty/option/parameter.rb', line 178

def validate(value = (not_set = true))
  if not_set
    @settings[:validate]
  else
    @settings[:validate] = check_validation(value)
  end
end

#validate?Boolean

Returns:

  • (Boolean)

186
187
188
# File 'lib/tty/option/parameter.rb', line 186

def validate?
  @settings.key?(:validate) && !@settings[:validate].nil?
end