Class: TaxJp::CorporateTaxes::DbBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tax_jp/corporate_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/corporate_taxes/db_builder.rb', line 6

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

Instance Method Details

#run(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tax_jp/corporate_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 = TaxJp::Utils.filename_to_date(filename)

      CSV.foreach(filename, :col_sep => "\t", :headers => true) do |row|
        next if row[0].to_s.strip.empty?

        values = []
        values << valid_from
        values << valid_until
        values << row[0].strip
        values << row[1].strip
        values << row[2].strip
        values << row[3].strip
        db.execute(insert_sql_applicable_item, values)
      end
    end
  end
end