Class: Reportier::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/reportier/tracker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Tracker

Returns a new instance of Tracker.



12
13
14
15
16
17
18
19
20
# File 'lib/reportier/tracker.rb', line 12

def initialize(opts = {})
  @type         = opts[:type]
  @name         = Namer.new.name \
    "#{opts[:name]}#{@type && @type.capitalize}Tracker"
  @defaults     = Defaults.global
  @reporter     = opts[:reporter]  || Reporter.get
  @persister    = opts[:persister] || Persister.get(self)
  @persister.set_date(_set_date)
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/reportier/tracker.rb', line 4

def defaults
  @defaults
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/reportier/tracker.rb', line 4

def name
  @name
end

#persisterObject (readonly)

Returns the value of attribute persister.



4
5
6
# File 'lib/reportier/tracker.rb', line 4

def persister
  @persister
end

#reporterObject (readonly)

Returns the value of attribute reporter.



4
5
6
# File 'lib/reportier/tracker.rb', line 4

def reporter
  @reporter
end

#started_atObject

Returns the value of attribute started_at.



3
4
5
# File 'lib/reportier/tracker.rb', line 3

def started_at
  @started_at
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/reportier/tracker.rb', line 4

def type
  @type
end

Class Method Details

.[](type) ⇒ Object



7
8
9
10
# File 'lib/reportier/tracker.rb', line 7

def self.[](type)
  @current       ||= Hash.new
  @current[type] ||= new(type: type)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/reportier/tracker.rb', line 35

def active?
  DateTime.now < expires_at
end

#add(item) ⇒ Object



22
23
24
25
26
# File 'lib/reportier/tracker.rb', line 22

def add(item)
  (report && clear) unless active?
  return if Defaults.global.trackers[@type] == 0
  @persister.save(Namer.new.name_item(item))
end

#reportObject



28
29
30
31
32
33
# File 'lib/reportier/tracker.rb', line 28

def report
  @reporter.call(self) do
    "#{@type} report started at #{@started_at}\n" +
    @persister.report
  end
end

#to_hashObject



43
44
45
# File 'lib/reportier/tracker.rb', line 43

def to_hash
  @persister.to_hash
end

#to_jsonObject



39
40
41
# File 'lib/reportier/tracker.rb', line 39

def to_json
  @persister.to_json
end