Class: LucaSalary::State

Inherits:
LucaRecord::Base
  • Object
show all
Includes:
Accumulator
Defined in:
lib/luca_salary/state.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accumulator

included

Constructor Details

#initialize(data, count = nil, start_d: nil, end_d: nil) ⇒ State

Returns a new instance of State.



12
13
14
15
16
17
18
# File 'lib/luca_salary/state.rb', line 12

def initialize(data, count = nil, start_d: nil, end_d: nil)
  @monthly = data
  @count = count
  @start_date = start_d
  @end_date = end_d
  @dict = LucaRecord::Dict.load_tsv_dict(Pathname(LucaRecord::CONST.pjdir) / 'dict' / 'code.tsv')
end

Class Method Details

.range(from_year, from_month, to_year = from_year, to_month = from_month) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/luca_salary/state.rb', line 20

def self.range(from_year, from_month, to_year = from_year, to_month = from_month)
  date = Date.new(from_year.to_i, from_month.to_i, -1)
  last_date = Date.new(to_year.to_i, to_month.to_i, -1)
  raise 'invalid term specified' if date > last_date

  counts = []
  reports = [].tap do |r|
    while date <= last_date do
      slips = term(date.year, date.month, date.year, date.month)
      record, count = accumulate(slips)
      r << record.tap { |c| c['_d'] = date.to_s }
      counts << count
      date = Date.new(date.next_month.year, date.next_month.month, -1)
    end
  end
  new(reports, counts,
      start_d: Date.new(from_year.to_i, from_month.to_i, 1),
      end_d: Date.new(to_year.to_i, to_month.to_i, -1)
     )
end

Instance Method Details

#reportObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/luca_salary/state.rb', line 41

def report()
  total, _count = LucaSalary::State.accumulate(@monthly)
  total['_d'] = 'total'
  [@monthly, total].flatten.map do |m|
    {}.tap do |r|
      m.sort.to_h.each do |k, v|
        r["#{@dict.dig(k, :label) || k}"] = readable(v)
      end
    end
  end
end