Class: SpsBill::BillCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/sps_bill/bill_collection.rb

Overview

SpsBill::BillCollection is an Array-like class that represents a collection of SP Services PDF bills.

The load method is used to initialise the collection given a path specification.

A range of collection methods are provided to extract sets of data from the entire collection (e.g. electricity_usages).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(path_spec) ⇒ Object

Returns an array of Bill objects for PDF files matching path_spec. path_spec may be either:

  • an array of filenames e.g. [‘data/file1.pdf’,‘file2.pdf’]

  • or a single file or path spec e.g. ‘./somepath/file1.pdf’ or ‘./somepath/*.pdf’



17
18
19
20
21
22
# File 'lib/sps_bill/bill_collection.rb', line 17

def load(path_spec)
  path_spec = Dir[path_spec] unless path_spec.class <= Array
  path_spec.each_with_object(new) do |filename,memo|
    memo << SpsBill::Bill.new(filename)
  end
end

Instance Method Details

#all_dataObject

Returns an array of all data by month

[[month,measure,kwh,cubic_m,rate,amount]]

measure: total_charges,electricity,gas,water



45
46
47
# File 'lib/sps_bill/bill_collection.rb', line 45

def all_data
  total_amounts(:all) + electricity_usages(:all) + gas_usages(:all) + water_usages(:all)
end

#electricity_usages(style = :solo) ⇒ Object

Returns an array of electricity_usages by month

[[month,kwh,rate,amount]]

when style is :solo, returns minimal array to describe this data set in isolation, else returns a normalised sparse array that is common to all data sets



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sps_bill/bill_collection.rb', line 67

def electricity_usages(style=:solo)
  each_with_object([]) do |bill,memo|
    bill.electricity_usage.each do |usage|
      if style==:solo
        memo << [bill.invoice_month.to_s,usage[:kwh],usage[:rate],usage[:amount]]
      else
        memo << [bill.invoice_month.to_s,'electricity',usage[:kwh],nil,usage[:rate],usage[:amount]]
      end
    end
  end
end

#gas_usages(style = :solo) ⇒ Object

Returns an array of gas_usages by month

[[month,kwh,rate,amount]]

when style is :solo, returns minimal array to describe this data set in isolation, else returns a normalised sparse array that is common to all data sets



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sps_bill/bill_collection.rb', line 83

def gas_usages(style=:solo)
  each_with_object([]) do |bill,memo|
    bill.gas_usage.each do |usage|
      if style==:solo
        memo << [bill.invoice_month.to_s,usage[:kwh],usage[:rate],usage[:amount]]
      else
        memo << [bill.invoice_month.to_s,'gas',usage[:kwh],nil,usage[:rate],usage[:amount]]
      end
    end
  end
end

#headers(dataset_selector) ⇒ Object

Returns the suitable array of headers for dataset_selector



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sps_bill/bill_collection.rb', line 27

def headers(dataset_selector)
  case dataset_selector
  when :total_amounts
    %w(invoice_month amount)
  when :electricity_usages
    %w(invoice_month kwh rate amount)
  when :gas_usages
    %w(invoice_month kwh rate amount)
  when :water_usages
    %w(invoice_month cubic_m rate amount)
  when :all_data
    %w(invoice_month measure kwh cubic_m rate amount)
  end
end

#total_amounts(style = :solo) ⇒ Object

Returns an array of total bill amounts by month

[[month,amount]]

when style is :solo, returns minimal array to describe this data set in isolation, else returns a normalised sparse array that is common to all data sets



53
54
55
56
57
58
59
60
61
# File 'lib/sps_bill/bill_collection.rb', line 53

def total_amounts(style=:solo)
  each_with_object([]) do |bill,memo|
    if style==:solo
      memo << [bill.invoice_month.to_s,bill.total_amount]
    else
      memo << [bill.invoice_month.to_s,'total_charges',nil,nil,nil,bill.total_amount]
    end
  end
end

#water_usages(style = :solo) ⇒ Object

Returns an array of water_usages by month

[[month,kwh,rate,amount]]

when style is :solo, returns minimal array to describe this data set in isolation, else returns a normalised sparse array that is common to all data sets



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sps_bill/bill_collection.rb', line 99

def water_usages(style=:solo)
  each_with_object([]) do |bill,memo|
    bill.water_usage.each do |usage|
      if style==:solo
        memo << [bill.invoice_month.to_s,usage[:cubic_m],usage[:rate],usage[:amount]]
      else
        memo << [bill.invoice_month.to_s,'water',nil,usage[:cubic_m],usage[:rate],usage[:amount]]
      end
    end
  end
end