Class: Aspera::Cli::ExtendedValue

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/aspera/cli/extended_value.rb

Overview

command line extended values

Constant Summary collapse

MARKER_START =
'@'
MARKER_END =
':'
MARKER_IN_END =
'@'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert_no_value(v, what) ⇒ Object



41
42
43
# File 'lib/aspera/cli/extended_value.rb', line 41

def assert_no_value(v, what)
  raise "no value allowed for extended value type: #{what}" unless v.empty?
end

.decode_csvt(value) ⇒ Object

decode comma separated table text



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aspera/cli/extended_value.rb', line 26

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 line') if hash_array.empty?
  return hash_array
end

Instance Method Details

#default_decoder=(value) ⇒ Object



82
83
84
85
86
# File 'lib/aspera/cli/extended_value.rb', line 82

def default_decoder=(value)
  Log.log.debug{"setting default decoder to #{value} (#{value.class})"}
  Aspera.assert(value.nil? || @handlers.key?(value))
  @default_decoder = value
end

#evaluate(value) ⇒ Object

parse an string value to extended value, if it is a String using supported extended value modifiers other value types are returned as is

Parameters:

  • value (String)

    the value to parse

  • expect (Class, Array)

    one or a list of expected types



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aspera/cli/extended_value.rb', line 101

def evaluate(value)
  return value unless value.is_a?(String)
  regex = Regexp.new("^#{handler_regex_string}(.*)$", Regexp::MULTILINE)
  # first determine decoders, in reversed order
  handlers_reversed = []
  while (m = value.match(regex))
    handler = m[1].to_sym
    handlers_reversed.unshift(handler)
    value = m[2]
    # stop processing if handler is extend (it will be processed later)
    break if handler.eql?(:extend)
  end
  Log.log.trace1{"evaluating: #{handlers_reversed}, value: #{value}"}
  handlers_reversed.each do |handler|
    value = @handlers[handler].call(value)
  end
  return value
end

#evaluate_all(value) ⇒ Object

find inner extended values



130
131
132
133
134
135
136
137
138
# File 'lib/aspera/cli/extended_value.rb', line 130

def evaluate_all(value)
  regex = Regexp.new("^(.*)#{handler_regex_string}([^#{MARKER_IN_END}]*)#{MARKER_IN_END}(.*)$", Regexp::MULTILINE)
  while (m = value.match(regex))
    sub_value = "@#{m[2]}:#{m[3]}"
    Log.log.debug{"evaluating #{sub_value}"}
    value = m[1] + evaluate(sub_value) + m[4]
  end
  return value
end

#evaluate_with_default(value) ⇒ Object

parse string value as extended value use default decoder if none is specified



122
123
124
125
126
127
# File 'lib/aspera/cli/extended_value.rb', line 122

def evaluate_with_default(value)
  if value.is_a?(String) && value.match(/^#{handler_regex_string}.*$/).nil? && !@default_decoder.nil?
    value = [MARKER_START, @default_decoder, MARKER_END, value].join
  end
  return evaluate(value)
end

#modifiersObject



88
# File 'lib/aspera/cli/extended_value.rb', line 88

def modifiers; @handlers.keys; end

#set_handler(name, method) ⇒ Object

add a new handler



91
92
93
94
95
# File 'lib/aspera/cli/extended_value.rb', line 91

def set_handler(name, method)
  Log.log.debug{"setting handler for #{name}"}
  Aspera.assert_type(name, Symbol){'name'}
  @handlers[name] = method
end