Module: Avalon::Extractable

Included in:
Block, Miner
Defined in:
lib/avalon/extractable.rb

Overview

Mix-in for extraction of properties from a given input String or Hash

Instance Method Summary collapse

Instance Method Details

#extract_data_from(input) ⇒ Object

Extract data from String OR Hash



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/avalon/extractable.rb', line 26

def extract_data_from input
  if input.nil? || input.empty?
    {}
  else
    # Convert the input into usable data pairs
    pairs = self::FIELDS.map do |name, (_, pattern, type)|
      val = input[pattern] # works for both pattern and key
      unless val.nil?
        case type
        when Symbol
          [name, val.send("to_#{type}")]
        when Proc
          [name, type.call(val)]
        when '' # no conversion
          [name, val]
        else
          nil
        end
      end
    end
    Hash[*pairs.compact.flatten]
  end
end

#my_time(t, type = :absolute_time) ⇒ Object

type = :absolute_time | :absolute_date | :relative_time



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/avalon/extractable.rb', line 7

def my_time t, type=:absolute_time
  t = t.to_f < 0 ? 0 : t.to_f
  time = Time.at(t)
  case type
  when :absolute_date
    time.getlocal.strftime("%Y-%m-%d %H:%M:%S")
  when :absolute_time
    time.getlocal.strftime("%H:%M:%S")
  when :relative_time
    time.utc.strftime("#{(time.day-1)*24+time.hour}:%M:%S")
  end
end


20
21
22
# File 'lib/avalon/extractable.rb', line 20

def print_headers
  puts self::FIELDS.map {|name, (width,_,_ )| name.to_s.ljust(width)}.join(' ')
end