Class: Aspera::Cli::OptionValue

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/cli/manager.rb

Overview

Description of option, how to manage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: nil, schema: nil) ⇒ OptionValue

‘allowed`:

  • ‘nil` No validation, so just a string

  • ‘Class` The single allowed Class

  • ‘Array<Class>` Multiple allowed classes

  • ‘Array<Symbol>` List of allowed values

Parameters:

  • option (Symbol)

    Name of option

  • description (String)

    Description for help

  • allowed (nil, Class, Array<Class>, Array<Symbol>) (defaults to: Allowed::TYPES_STRING)

    Allowed values

  • handler (Hash) (defaults to: nil)

    Accessor: keys: :o(object) and :m(method)

  • deprecation (String) (defaults to: nil)

    Deprecation message

  • schema (String) (defaults to: nil)

    Declaration of schema



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/aspera/cli/manager.rb', line 96

def initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: nil, schema: nil)
  Log.log.trace1{"option: #{option}, allowed: #{allowed}"}
  @option = option
  @description = description
  # by default passwords and secrets are sensitive, else specify when declaring the option
  @sensitive = SecretHider.instance.secret?(@option, '')
  # either the value, or object giving value
  @object = handler&.[](:o)
  @read_method = handler&.[](:m)
  @write_method = @read_method ? "#{@read_method}=".to_sym : nil
  @deprecation = deprecation
  @schema = schema
  @access = if @object.nil?
    :local
  elsif @object.respond_to?(@write_method)
    :write
  else
    :setter
  end
  Aspera.assert(@object.respond_to?(@read_method)){"#{@object} does not respond to #{@read_method}"} unless @access.eql?(:local)
  @types = nil
  @values = nil
  if !allowed.nil?
    allowed = [allowed] if allowed.is_a?(Class)
    Aspera.assert_type(allowed, Array)
    if allowed.take(Allowed::TYPES_SYMBOL_ARRAY.length) == Allowed::TYPES_SYMBOL_ARRAY
      # Special case: array of defined symbol values
      @types = Allowed::TYPES_SYMBOL_ARRAY
      @values = allowed[Allowed::TYPES_SYMBOL_ARRAY.length..]
    elsif allowed.all?(Class)
      @types = allowed
      @values = BoolValue::ALL if allowed.eql?(Allowed::TYPES_BOOLEAN)
      # Default value for array
      @object ||= [] if @types.first.eql?(Array) && !@types.include?(NilClass)
      @object ||= {} if @types.first.eql?(Hash) && !@types.include?(NilClass)
    elsif allowed.all?(Symbol)
      @types = Allowed::TYPES_ENUM
      @values = allowed
    else
      Aspera.error_unexpected_value(allowed)
    end
  end
  Log.log.trace1{"declare: #{@option}: #{@access} #{@object.class}.#{@read_method}".green}
end

Instance Attribute Details

#optionObject (readonly)

Array(Class)

List of allowed types



81
82
83
# File 'lib/aspera/cli/manager.rb', line 81

def option
  @option
end

#schemaObject (readonly)

Array(Class)

List of allowed types



81
82
83
# File 'lib/aspera/cli/manager.rb', line 81

def schema
  @schema
end

#sensitiveObject (readonly)

Array(Class)

List of allowed types



81
82
83
# File 'lib/aspera/cli/manager.rb', line 81

def sensitive
  @sensitive
end

#typesObject (readonly)

Array(Class)

List of allowed types



81
82
83
# File 'lib/aspera/cli/manager.rb', line 81

def types
  @types
end

#valuesObject

Array

List of allowed values (Symbols and specific values)



83
84
85
# File 'lib/aspera/cli/manager.rb', line 83

def values
  @values
end

Instance Method Details

#assign_value(value, where:) ⇒ nil

Assign value to option. Value can be a ‘String`, then evaluated with `ExtendedValue`, or directly a value.

Parameters:

  • value (String, Object)

    Value to assign to option

  • where (String)

    Where the value is assigned from

Returns:

  • (nil)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/aspera/cli/manager.rb', line 161

def assign_value(value, where:)
  Aspera.assert(!@deprecation, type: warn){"Option #{@option} is deprecated: #{@deprecation}"}
  new_value = ExtendedValue.instance.evaluate(value, context: "option: #{@option}", allowed: @types)
  Log.log.trace1{"#{where}: #{@option} <- (#{new_value.class})#{new_value}"}
  new_value = BoolValue.true?(new_value) if @types.eql?(Allowed::TYPES_BOOLEAN)
  new_value = Integer(new_value) if @types.eql?(Allowed::TYPES_INTEGER)
  new_value = [new_value] if @types.eql?(Allowed::TYPES_STRING_ARRAY) && new_value.is_a?(String)
  # Setting a Hash to null set an empty hash
  new_value = {} if new_value.eql?(nil) && @types&.first.eql?(Hash)
  # Setting a Array to null set an empty array
  new_value = [] if new_value.eql?(nil) && @types&.first.eql?(Array)
  if @types.eql?(Aspera::Cli::Allowed::TYPES_SYMBOL_ARRAY)
    new_value = [new_value] if new_value.is_a?(String)
    Aspera.assert_array_all(new_value, String, type: BadArgument)
    new_value = new_value.map{ |v| Manager.get_from_list(v, @option, @values)}
  end
  Aspera.assert_type(new_value, *@types, type: BadArgument){"Option #{@option}"} if @types
  if new_value.is_a?(Hash) || new_value.is_a?(Array)
    current_value = value(log: false)
    new_value = current_value.deep_merge(new_value) if new_value.is_a?(Hash) && current_value.is_a?(Hash) && !current_value.empty?
    new_value = current_value + new_value if new_value.is_a?(Array) && current_value.is_a?(Array) && !current_value.empty?
  end
  case @access
  when :local then @object = new_value
  when :write then @object.send(@write_method, new_value)
  when :setter then @object.send(@read_method, @option, :set, new_value)
  end
  Log.log.trace1{v = value(log: false); "#{@option} <- (#{v.class})#{v}"} # rubocop:disable Style/Semicolon
  nil
end

#clearObject



141
142
143
# File 'lib/aspera/cli/manager.rb', line 141

def clear
  @object = nil
end

#value(log: true) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/aspera/cli/manager.rb', line 145

def value(log: true)
  current_value =
    case @access
    when :local then @object
    when :write then @object.send(@read_method)
    when :setter then @object.send(@read_method, @option, :get)
    end
  Log.log.trace1{"#{@option} -> (#{current_value.class})#{current_value}"} if log
  current_value
end