Class: Aspera::Cli::OptionValue
- Inherits:
-
Object
- Object
- Aspera::Cli::OptionValue
- Defined in:
- lib/aspera/cli/manager.rb
Overview
Description of option, how to manage
Instance Attribute Summary collapse
-
#sensitive ⇒ Object
readonly
- Array(Class)
-
List of allowed types.
-
#types ⇒ Object
readonly
- Array(Class)
-
List of allowed types.
-
#values ⇒ Object
- Array
-
List of allowed values (Symbols and specific values).
Instance Method Summary collapse
-
#assign_value(value, where:) ⇒ Object
Assign value to option.
- #clear ⇒ Object
-
#initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: nil) ⇒ OptionValue
constructor
allowed: -nilNo validation, so just a string -ClassThe single allowed Class - ‘Array<Class>` Multiple allowed classes - `Array<Symbol>` List of allowed values. - #value(log: true) ⇒ Object
Constructor Details
#initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: nil) ⇒ OptionValue
allowed:
-
nilNo validation, so just a string -
ClassThe single allowed Class -
‘Array<Class>` Multiple allowed classes
-
‘Array<Symbol>` List of allowed values
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 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 |
# File 'lib/aspera/cli/manager.rb', line 78 def initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: 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 @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..-1] 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
#sensitive ⇒ Object (readonly)
- Array(Class)
-
List of allowed types
64 65 66 |
# File 'lib/aspera/cli/manager.rb', line 64 def sensitive @sensitive end |
#types ⇒ Object (readonly)
- Array(Class)
-
List of allowed types
64 65 66 |
# File 'lib/aspera/cli/manager.rb', line 64 def types @types end |
#values ⇒ Object
- Array
-
List of allowed values (Symbols and specific values)
66 67 68 |
# File 'lib/aspera/cli/manager.rb', line 66 def values @values end |
Instance Method Details
#assign_value(value, where:) ⇒ Object
Assign value to option. Value can be a String, then evaluated with ExtendedValue, or directly a value.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/aspera/cli/manager.rb', line 140 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 hash 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_type(new_value, Array, type: BadArgument) 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 end |
#clear ⇒ Object
122 123 124 |
# File 'lib/aspera/cli/manager.rb', line 122 def clear @object = nil end |
#value(log: true) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/aspera/cli/manager.rb', line 126 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 return current_value end |