Class: Edj::Journal

Inherits:
Object
  • Object
show all
Defined in:
lib/edj/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ Journal

Returns a new instance of Journal.



31
32
33
34
# File 'lib/edj/base.rb', line 31

def initialize(f)
  @f = f
  @tree = {}
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



29
30
31
# File 'lib/edj/base.rb', line 29

def tree
  @tree
end

Instance Method Details

#filter(type) ⇒ Object



36
37
38
39
40
# File 'lib/edj/base.rb', line 36

def filter(type)
  File.open(@f).readlines
    .map {|line| Item.from(JSON.parse(line))}
    .select {|item| item.is_a? type }
end

#scan_valueObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/edj/base.rb', line 42

def scan_value()
  total_value = 0
  File.open(@f).each do |line|
    item = Item.from(JSON.parse(line))
    if item.is_a? ScanItem then
      v = item.value
      bits = item.bodyName.split(' ')
      (1..(bits.size-1)).each do |i|
        entry = bits[0..i].join(' ')
        o = @tree[entry]
        @tree[entry] ||= 0
        @tree[entry] += v
      end
      STDOUT.puts("#{item.timestamp} #{item.bodyName} --> #{v}")
      total_value = total_value + v
    end
  end
  total_value
end