Class: TaxJp::Utils
- Inherits:
-
Object
- Object
- TaxJp::Utils
- Defined in:
- lib/tax_jp/utils.rb
Class Method Summary collapse
- .convert_to_date(value) ⇒ Object
- .convert_to_zip_code(value) ⇒ Object
- .data_dir ⇒ Object
- .filename_to_date(filename) ⇒ Object
- .gem_dir ⇒ Object
- .load_file(filename) ⇒ Object
- .load_yaml(filename) ⇒ Object
- .normalize_amount(amount, options = {}) ⇒ Object
- .render(filename) ⇒ Object
- .with_database(db_path, results_as_hash: false) ⇒ Object
Class Method Details
.convert_to_date(value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tax_jp/utils.rb', line 41 def convert_to_date(value) ret = nil if value.is_a?(Date) ret = value.strftime('%Y-%m-%d') elsif value.is_a?(String) if value =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/ ret = value else raise ArgumentError.new(value) end else raise TypeError.new(value.class) end ret end |
.convert_to_zip_code(value) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tax_jp/utils.rb', line 59 def convert_to_zip_code(value) ret = nil if value.is_a?(String) ret = value.sub('-', '') else raise TypeError.new(value.class) end ret end |
.data_dir ⇒ Object
11 12 13 |
# File 'lib/tax_jp/utils.rb', line 11 def data_dir File.join(gem_dir, 'data') end |
.filename_to_date(filename) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/tax_jp/utils.rb', line 71 def filename_to_date(filename) _, valid_from, valid_until = File.basename(filename).split('.').first.split('-') valid_from = Date.strptime(valid_from, '%Y%m%d') valid_until = Date.strptime(valid_until, '%Y%m%d') [valid_from.strftime('%Y-%m-%d'), valid_until.strftime('%Y-%m-%d')] end |
.gem_dir ⇒ Object
7 8 9 |
# File 'lib/tax_jp/utils.rb', line 7 def gem_dir File.('../../..', __FILE__) end |
.load_file(filename) ⇒ Object
19 20 21 |
# File 'lib/tax_jp/utils.rb', line 19 def load_file(filename) File.read(File.join(data_dir, filename)) end |
.load_yaml(filename) ⇒ Object
15 16 17 |
# File 'lib/tax_jp/utils.rb', line 15 def load_yaml(filename) YAML.load(File.read(File.join(data_dir, filename))) end |
.normalize_amount(amount, options = {}) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tax_jp/utils.rb', line 78 def normalize_amount(amount, = {}) ret = amount.to_s if ret == '-' ret = TaxJp::INTEGER_MAX else ret = ret.gsub(',', '') if ret.index('.') ret = ret.to_f else ret = ret.to_i end end ret end |
.render(filename) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/tax_jp/utils.rb', line 23 def render(filename) src = File.join(gem_dir, 'templates', filename + '.erb') dest = File.join(gem_dir, filename) FileUtils.mkdir_p(File.dirname(dest)) File.write(dest, ERB.new(File.read(src), 0, '-').result) end |
.with_database(db_path, results_as_hash: false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tax_jp/utils.rb', line 30 def with_database(db_path, results_as_hash: false) db = SQLite3::Database.new(db_path) db.results_as_hash = results_as_hash begin yield db ensure db.close end end |