Class: TaxJp::WithheldTaxes::DbBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tax_jp/withheld_taxes/db_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_path = nil) ⇒ DbBuilder

Returns a new instance of DbBuilder.



6
7
8
# File 'lib/tax_jp/withheld_taxes/db_builder.rb', line 6

def initialize(db_path = nil)
  @db_path = db_path || TaxJp::WithheldTax::DB_PATH
end

Instance Method Details

#run(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tax_jp/withheld_taxes/db_builder.rb', line 10

def run(options = {})
  with_database(options) do |db|
    Dir.glob(File.join(TaxJp::Utils.data_dir, '源泉徴収税', '*.tsv')).each do |filename|
      valid_from, valid_until = filename_to_date(filename)

      CSV.foreach(filename, :col_sep => "\t") do |row|
        next if row[0].nil?
        db.execute(insert_sql, [valid_from.to_s, valid_until.to_s] + row.map{|col| normalize_amount(col)})
      end
    end
  end
end