Class: RShade::Formatter::Json

Inherits:
Object
  • Object
show all
Defined in:
lib/rshade/formatter/json.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_processorObject (readonly)

Returns the value of attribute event_processor.



4
5
6
# File 'lib/rshade/formatter/json.rb', line 4

def event_processor
  @event_processor
end

Instance Method Details

#call(event_store) ⇒ Object

Parameters:



7
8
9
10
# File 'lib/rshade/formatter/json.rb', line 7

def call(event_store)
  @event_store = event_store
  flat
end

#flatObject



12
13
14
15
16
17
18
# File 'lib/rshade/formatter/json.rb', line 12

def flat
  arr = []
  event_store.each do |node|
    arr << item(node.event)
  end
  arr.sort_by { |item| item[:level]}
end

#hash_iterate(hash, depth) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rshade/formatter/json.rb', line 38

def hash_iterate(hash, depth)
  (0..depth).each do |lvl|
    unless hash[:inner]
      hash[:inner] = {}
    end
    hash = hash[:inner]
  end
  hash
end

#hierarchicalObject



20
21
22
23
24
25
26
27
28
# File 'lib/rshade/formatter/json.rb', line 20

def hierarchical
  hash = {}
  event_store.each do |node|
    depth = node.level
    ref = hash_iterate(hash, depth)
    ref[:data] = item(node)
  end
  sort_hash(hash)
end

#item(value) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/rshade/formatter/json.rb', line 48

def item(value)
  {
    class: value.klass.to_s,
    method_name: value.method_name,
    full_path: "#{value.path}:#{value.lineno}",
    level: value.depth,
    vars: value.vars
  }
end

#sort_hash(h) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/rshade/formatter/json.rb', line 30

def sort_hash(h)
  {}.tap do |h2|
    h.sort.each do |k,v|
      h2[k] = v.is_a?(Hash) ? sort_hash(v) : v
    end
  end
end