lucarecord 0.2.16 → 0.2.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 222229f2cc5299302f8ff1df81a92ce9a507101e6035765cdaa1408ad8e93365
4
- data.tar.gz: 30102479db3e25fca972cf723d8f679891a179ed0b9ceb79cb03d2b22f25743b
3
+ metadata.gz: 25005e140ff2445d9a7c39bb8805f94373d0cf5fdc74996248537c8493996a76
4
+ data.tar.gz: 675eb686f9565bf73319117f41f6ea0f4e1a0202250cc8987f67aaa7a7cf677a
5
5
  SHA512:
6
- metadata.gz: 4be9918878c1d9204ccbaac61cfefac3872b028d192e8013d177fda369092dcd1ff2884e7e0e53a1384e3e9b21f57cbca903b6f448bddb39ec2199dd4aefdf46
7
- data.tar.gz: b5730ce678fc0259a4550f14fddce5655509d07653c0fd8d405d9d1f0f3e48572cad1818790e29ead2d23144be95a3b9229aaf424bca4b71b61c12603b16c0fc
6
+ metadata.gz: fb805203d9ff072caf90e281e5dbf4b062455b1707925cd16a319735dea95c346c6b959c2c4633c59630fcbc99209bfb7477e6764453dcad5bfac6bd68e68302
7
+ data.tar.gz: b374ae84672a08400ed074f6a150780da0ae29558941178db941b99e0074c6e51e34245aeb4865fac0252a601d5f677b094f3e827e5bb91030e6cd88468096b1
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bigdecimal'
3
4
  require 'csv'
4
5
  require 'date'
5
6
  require 'fileutils'
@@ -95,7 +96,7 @@ module LucaRecord # :nodoc:
95
96
  id = LucaSupport::Code.issue_random_id
96
97
  obj['id'] = id
97
98
  open_hashed(basedir, id, 'w') do |f|
98
- f.write(YAML.dump(obj.sort.to_h))
99
+ f.write(YAML.dump(LucaSupport::Code.readable(obj.sort.to_h)))
99
100
  end
100
101
  id
101
102
  end
@@ -103,7 +104,7 @@ module LucaRecord # :nodoc:
103
104
  # define new transaction ID & write data at once
104
105
  def create_record!(obj, date_obj, codes = nil, basedir = @dirname)
105
106
  gen_record_file!(basedir, date_obj, codes) do |f|
106
- f.write(YAML.dump(obj.sort.to_h))
107
+ f.write(YAML.dump(LucaSupport::Code.readable(obj.sort.to_h)))
107
108
  end
108
109
  end
109
110
 
@@ -129,7 +130,7 @@ module LucaRecord # :nodoc:
129
130
  else
130
131
  validate_keys(obj)
131
132
  open_hashed(basedir, obj['id'], 'w') do |f|
132
- f.write(YAML.dump(obj.sort.to_h))
133
+ f.write(YAML.dump(LucaSupport::Code.readable(obj.sort.to_h)))
133
134
  end
134
135
  end
135
136
  obj['id']
@@ -255,6 +256,7 @@ module LucaRecord # :nodoc:
255
256
  # TODO: implement JSON parse
256
257
  else
257
258
  YAML.load(io.read).tap { |obj| validate_keys(obj) }
259
+ .inject({}) { |h, (k, v)| h[k] = LucaSupport::Code.decimalize(v); h }
258
260
  end
259
261
  end
260
262
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaRecord
4
- VERSION = '0.2.16'
4
+ VERSION = '0.2.17'
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require 'date'
4
4
  require 'securerandom'
5
5
  require 'digest/sha1'
6
+ require 'luca_support/config'
6
7
 
7
8
  # implement Luca IDs convention
8
9
  module LucaSupport
@@ -72,6 +73,37 @@ module LucaSupport
72
73
  Digest::SHA1.hexdigest(SecureRandom.uuid)
73
74
  end
74
75
 
76
+ def decimalize(obj)
77
+ case obj.class.name
78
+ when 'Array'
79
+ obj.map { |i| decimalize(i) }
80
+ when 'Hash'
81
+ obj.inject({}) { |h, (k, v)| h[k] = decimalize(v); h }
82
+ when 'Integer'
83
+ BigDecimal(obj.to_s)
84
+ when 'String'
85
+ /^[0-9\.]+$/.match(obj) ? BigDecimal(obj) : obj
86
+ when 'Float'
87
+ raise 'already float'
88
+ else
89
+ obj
90
+ end
91
+ end
92
+
93
+ def readable(obj, len = LucaSupport::Config::DECIMAL_NUM)
94
+ case obj.class.name
95
+ when 'Array'
96
+ obj.map { |i| readable(i) }
97
+ when 'Hash'
98
+ obj.inject({}) { |h, (k, v)| h[k] = readable(v); h }
99
+ when 'BigDecimal'
100
+ parts = obj.round(len).to_s('F').split('.')
101
+ len < 1 ? parts.first : "#{parts[0]}.#{parts[1][0, len]}"
102
+ else
103
+ obj
104
+ end
105
+ end
106
+
75
107
  #
76
108
  # convert effective/defunct data into current hash on @date.
77
109
  # not parse nested children.
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pathname'
4
+ require 'yaml'
5
+
3
6
  #
4
7
  # startup config
5
8
  #
@@ -7,5 +10,11 @@ module LucaSupport
7
10
  module Config
8
11
  # Project top directory.
9
12
  Pjdir = ENV['LUCA_TEST_DIR'] || Dir.pwd.freeze
13
+ if File.exist?(Pathname(Pjdir) / 'config.yml')
14
+ DECIMAL_NUM = YAML.load_file(Pathname(Pjdir) / 'config.yml', **{})['decimal_number']
15
+ COUNTRY = YAML.load_file(Pathname(Pjdir) / 'config.yml', **{})['country']
16
+ DECIMAL_NUM ||= 0 if COUNTRY == 'jp'
17
+ end
18
+ DECIMAL_NUM ||= 2
10
19
  end
11
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucarecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.16
4
+ version: 0.2.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro