Class: Aspera::Cli::ExtendedValue
- Inherits:
-
Object
- Object
- Aspera::Cli::ExtendedValue
- Includes:
- Singleton
- Defined in:
- lib/aspera/cli/extended_value.rb
Overview
Command line extended values
Constant Summary collapse
- DEFAULT_DECODERS =
First is default
%i[none json ruby yaml]
Instance Attribute Summary collapse
-
#default_decoder ⇒ Object
Returns the value of attribute default_decoder.
Class Method Summary collapse
-
.assert_no_value(value, ext_type) ⇒ Object
The value must be empty.
-
.decode_csvt(value) ⇒ Object
Decode comma separated table text.
-
.JSON_parse(value) ⇒ Object
JSON Parser, with more information on error location extract a context: 10 chars before and after the error on the given line and display a pointer “^” :reek:UncommunicativeMethodName.
Instance Method Summary collapse
-
#evaluate(value, context:, allowed: nil) ⇒ Object
Parses a ‘String` value to extended value.
-
#evaluate_extend(value) ⇒ Object
Find inner extended values Only used in above lambda.
-
#modifiers ⇒ Object
List of Extended Value methods.
-
#on(name, &block) ⇒ Object
Add a new handler.
Instance Attribute Details
#default_decoder ⇒ Object
Returns the value of attribute default_decoder.
111 112 113 |
# File 'lib/aspera/cli/extended_value.rb', line 111 def default_decoder @default_decoder end |
Class Method Details
.assert_no_value(value, ext_type) ⇒ Object
The value must be empty
64 65 66 |
# File 'lib/aspera/cli/extended_value.rb', line 64 def assert_no_value(value, ext_type) Aspera.assert(value.empty?, type: BadArgument){"no value allowed for extended value type: #{ext_type}"} end |
.decode_csvt(value) ⇒ Object
Decode comma separated table text
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/aspera/cli/extended_value.rb', line 25 def decode_csvt(value) col_titles = nil hash_array = [] CSV.parse(value).each do |values| next if values.empty? if col_titles.nil? col_titles = values else hash_array.push(col_titles.zip(values).to_h) end end Log.log.warn('Titled CSV file without any row') if hash_array.empty? return hash_array end |
.JSON_parse(value) ⇒ Object
JSON Parser, with more information on error location extract a context: 10 chars before and after the error on the given line and display a pointer “^” :reek:UncommunicativeMethodName
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/aspera/cli/extended_value.rb', line 43 def JSON_parse(value) # rubocop:disable Naming/MethodName JSON.parse(value) rescue JSON::ParserError => e m = /at line (\d+) column (\d+)/.match(e.) raise if m.nil? line = m[1].to_i - 1 column = m[2].to_i - 1 lines = value.lines raise if line >= lines.size error_line = lines[line].chomp context_col_beg = [column - 10, 0].max context_col_end = [column + 10, error_line.length].min context = error_line[context_col_beg...context_col_end] cursor_pos = column - context_col_beg pointer = ' ' * cursor_pos + '^'.blink raise BadArgument, "#{e.}\n#{context}\n#{pointer}" end |
Instance Method Details
#evaluate(value, context:, allowed: nil) ⇒ Object
Parses a ‘String` value to extended value. If it is a String using supported extended value modifiers, then evaluate them. Other value types are returned as is.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/aspera/cli/extended_value.rb', line 139 def evaluate(value, context:, allowed: nil) return value unless value.is_a?(String) Aspera.assert_array_all(allowed, Class) unless allowed.nil? # use default decoder if not an extended value and expect complex types using_default_decoder = allowed&.all?{ |t| DEFAULT_PARSER_TYPES.include?(t)} && !@regex_single.match?(value) && !@default_decoder.nil? value = [MARKER_START, @default_decoder, MARKER_END, value].join if using_default_decoder # First determine decoders, in reversed order handlers_reversed = [] while (m = value.match(@regex_single)) handler = m[1].to_sym handlers_reversed.unshift(handler) value = m[2] break if SPECIAL_HANDLERS.include?(handler) end Log.log.trace1{"evaluating: #{handlers_reversed}, value: #{value}"} handlers_reversed.each do |handler| value = @handlers[handler].call(value) rescue => e raise BadArgument, "Evaluation of #{handler} for #{context}: #{e.}" end return value end |
#evaluate_extend(value) ⇒ Object
Find inner extended values Only used in above lambda
164 165 166 167 168 169 170 171 |
# File 'lib/aspera/cli/extended_value.rb', line 164 def evaluate_extend(value) while (m = value.match(@regex_extend)) sub_value = "@#{m[2]}:#{m[3]}" Log.log.debug{"evaluating #{sub_value}"} value = "#{m[1]}#{evaluate(sub_value, context: 'composite extended value')}#{m[4]}" end return value end |
#modifiers ⇒ Object
List of Extended Value methods
121 |
# File 'lib/aspera/cli/extended_value.rb', line 121 def modifiers; @handlers.keys; end |