lucarecord 0.2.16 → 0.2.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/luca_record/io.rb +5 -3
- data/lib/luca_record/version.rb +1 -1
- data/lib/luca_support/code.rb +32 -0
- data/lib/luca_support/config.rb +9 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25005e140ff2445d9a7c39bb8805f94373d0cf5fdc74996248537c8493996a76
|
4
|
+
data.tar.gz: 675eb686f9565bf73319117f41f6ea0f4e1a0202250cc8987f67aaa7a7cf677a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb805203d9ff072caf90e281e5dbf4b062455b1707925cd16a319735dea95c346c6b959c2c4633c59630fcbc99209bfb7477e6764453dcad5bfac6bd68e68302
|
7
|
+
data.tar.gz: b374ae84672a08400ed074f6a150780da0ae29558941178db941b99e0074c6e51e34245aeb4865fac0252a601d5f677b094f3e827e5bb91030e6cd88468096b1
|
data/lib/luca_record/io.rb
CHANGED
@@ -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
|
|
data/lib/luca_record/version.rb
CHANGED
data/lib/luca_support/code.rb
CHANGED
@@ -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.
|
data/lib/luca_support/config.rb
CHANGED
@@ -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
|