Class: Eco::Data::CountTrace

Inherits:
Object
  • Object
show all
Includes:
Language::Delegation::DelegatingMissing
Defined in:
lib/eco/data/count_trace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Language::Delegation::DelegatingMissing

#respond_to_missing?

Constructor Details

#initialize(subject) ⇒ CountTrace

Returns a new instance of CountTrace.



10
11
12
13
# File 'lib/eco/data/count_trace.rb', line 10

def initialize(subject)
  @subject = subject
  @trace   = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Eco::Language::Delegation::DelegatingMissing

Instance Attribute Details

#traceObject (readonly)

Returns the value of attribute trace.



8
9
10
# File 'lib/eco/data/count_trace.rb', line 8

def trace
  @trace
end

Instance Method Details

#report!(msg: nil, level: :warn) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/eco/data/count_trace.rb', line 24

def report!(msg: nil, level: :warn)
  str_report(msg: msg).tap do |str|
    log(level.to_sym) { str }

    reset!
  end
end

#reset!Object



15
16
17
# File 'lib/eco/data/count_trace.rb', line 15

def reset!
  @trace.clear
end

#str_report(msg: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/eco/data/count_trace.rb', line 32

def str_report(msg: nil)
  return if trace.empty?

  msg  ||= ''
  sorted = trace.sort_by {|value, count| [count * -1, value]}

  msg << "\n  * "
  msg << sorted.map.with_index do |(value, count), idx|
    "#{idx}. (#{count}) '#{value}'"
  end.join("\n  * ")

  msg
end

#trace!(value, count: 1) ⇒ Object



19
20
21
22
# File 'lib/eco/data/count_trace.rb', line 19

def trace!(value, count: 1)
  trace[value] ||= 0
  trace[value]  += count
end