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
-
#option ⇒ Object
readonly
[Array(Class)] List of allowed types.
-
#schema ⇒ Object
readonly
[Array(Class)] List of allowed types.
-
#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:) ⇒ nil
Assign value to option.
- #clear ⇒ Object
-
#initialize(option:, description:, allowed: Allowed::TYPES_STRING, handler: nil, deprecation: nil, schema: 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, schema: nil) ⇒ OptionValue
allowed:
nilNo validation, so just a stringClassThe single allowed ClassArray<Class>Multiple allowed classesArray<Symbol>List of allowed values
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 140 |
# File 'lib/aspera/cli/manager.rb', line 97 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
#option ⇒ Object (readonly)
[Array(Class)] List of allowed types
82 83 84 |
# File 'lib/aspera/cli/manager.rb', line 82 def option @option end |
#schema ⇒ Object (readonly)
[Array(Class)] List of allowed types
82 83 84 |
# File 'lib/aspera/cli/manager.rb', line 82 def schema @schema end |
#sensitive ⇒ Object (readonly)
[Array(Class)] List of allowed types
82 83 84 |
# File 'lib/aspera/cli/manager.rb', line 82 def sensitive @sensitive end |
#types ⇒ Object (readonly)
[Array(Class)] List of allowed types
82 83 84 |
# File 'lib/aspera/cli/manager.rb', line 82 def types @types end |
#values ⇒ Object
[Array] List of allowed values (Symbols and specific values)
84 85 86 |
# File 'lib/aspera/cli/manager.rb', line 84 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.
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 191 |
# File 'lib/aspera/cli/manager.rb', line 162 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 |
#clear ⇒ Object
142 143 144 |
# File 'lib/aspera/cli/manager.rb', line 142 def clear @object = nil end |
#value(log: true) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/aspera/cli/manager.rb', line 146 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 |