Class: TTY::Option::Params

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tty/option/params.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, remaining: [], errors: []) ⇒ Params

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 Params



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tty/option/params.rb', line 33

def initialize(parameters, remaining: [], errors: [])
  @parameters = parameters
  @parameters.default_proc = ->(hash, key) do
    return hash[key] if hash.key?(key)

    case key
    when Symbol
      hash[key.to_s] if hash.key?(key.to_s)
    when String
      hash[key.to_sym] if hash.key?(key.to_sym)
    end
  end
  @remaining = remaining
  @errors = AggregateErrors.new(errors)
end

Instance Attribute Details

#errorsObject (readonly)

The parameter parsing errors



28
29
30
# File 'lib/tty/option/params.rb', line 28

def errors
  @errors
end

#remainingObject (readonly)

The remaining unparsed arguments



23
24
25
# File 'lib/tty/option/params.rb', line 23

def remaining
  @remaining
end

Class Method Details

.create(parameters = {}, remaining = [], errors = []) ⇒ Object



12
13
14
# File 'lib/tty/option/params.rb', line 12

def self.create(parameters = {}, remaining = [], errors = [])
  new(parameters, remaining: remaining, errors: errors)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



88
89
90
91
92
# File 'lib/tty/option/params.rb', line 88

def ==(other)
  return false unless other.kind_of?(TTY::Option::Params)

  @parameters == other.to_h
end

#[](key) ⇒ Object

Access a given value for a key



52
53
54
# File 'lib/tty/option/params.rb', line 52

def [](key)
  @parameters[key]
end

#[]=(key, value) ⇒ Object

Assign value to a key



59
60
61
# File 'lib/tty/option/params.rb', line 59

def []=(key, value)
  @parameters[key] = value
end

#fetch(key, *args, &block) ⇒ Object

Access a given value for a key



66
67
68
69
70
71
# File 'lib/tty/option/params.rb', line 66

def fetch(key, *args, &block)
  value = self[key]
  return value unless value.nil?

  @parameters.fetch(key, *args, &block)
end

#hashObject



95
96
97
# File 'lib/tty/option/params.rb', line 95

def hash
  @parameters.hash
end

#inspectString

String representation of this params

Returns:

  • (String)


108
109
110
# File 'lib/tty/option/params.rb', line 108

def inspect
  "#<#{self.class}#{to_h.inspect}>"
end

#merge(other_params) ⇒ Object



73
74
75
# File 'lib/tty/option/params.rb', line 73

def merge(other_params)
  @parameters.merge(other_params)
end

#merge!(other_params) ⇒ Object



77
78
79
# File 'lib/tty/option/params.rb', line 77

def merge!(other_params)
  @parameters.merge!(other_params)
end

#to_hObject



99
100
101
# File 'lib/tty/option/params.rb', line 99

def to_h
  @parameters.to_h
end

#to_sString

String representation of the parameters

Returns:

  • (String)


117
118
119
# File 'lib/tty/option/params.rb', line 117

def to_s
  to_h.to_s
end

#valid?Boolean

Check if params have any errors

Returns:

  • (Boolean)


84
85
86
# File 'lib/tty/option/params.rb', line 84

def valid?
  @errors.empty?
end