lucarecord 0.5.3 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf1f2de4d31ac30fa0e7e2a54d2391c04a0aec3008f0d141fd5d887b39f1e8eb
4
- data.tar.gz: f50d14c026db0bd94007bff1e35ab0ff65b6e6a34f97ce282fa8b2dd53adc909
3
+ metadata.gz: 4574038ef165ecd098bd37897f77a16edb5917aa6c157ce45a005598bc540af2
4
+ data.tar.gz: a2f22d1d3c9899e3c84c7662cb8859794541e31cbd116eae343deb4d4db3e415
5
5
  SHA512:
6
- metadata.gz: 152215ed2b331ea30afc08b137b938231d6b40c3254bc8ea99f6cb540bba5727cf3e7b043b5002872ce8c814716c266a2f6f536880f2fdb01d674af6da1e76c7
7
- data.tar.gz: 951a08656f3f50e95d58ad724c5f998f23e34d45c8453e79ff297ba059e4ed9e9f176455c1f335d8ed1a52e7545fbb7c5224a1a5862af81c4072ae4c66ac6900
6
+ metadata.gz: 05dccaddb053508040dfab00cd9451b20b10d3a9b78a5317609ab8e71a548109cee9c4ff2579132f2e7380bb470d8639e6ddfb80ffdb23e324c2971e89d596d0
7
+ data.tar.gz: e33237c779d1450bd6c1b9a1c208382a8eda87848da74117b36bc973b93c44b965e1970d8eea60eb0d83698361406700c4dbe1480d511bc0130ece78cf7c6cf3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## LucaRecord 0.5.5
2
+
3
+ * support JSON for `LucaRecord::Base.load_data()` w/ `@record_type = 'json'`
4
+
5
+ ## LucaRecord 0.5.4
6
+
7
+ * add `upsert()`
8
+ * `open_records()` supports git-like id sub directories
9
+ * Regard zero-prefixed code like '00123' as not BigDecimal but String on YAML load
10
+ * Add support table columns order to `LucaSupport::View.nushell()`
11
+ * Breaking change: `LucaSupport::View.nushell()` takes Ruby Array. YAML is not accepted anymore.
12
+
1
13
  ## LucaRecord 0.5.3
2
14
 
3
15
  * add `find_secure()` for partial data encryption.
@@ -4,6 +4,7 @@ require 'bigdecimal'
4
4
  require 'csv'
5
5
  require 'date'
6
6
  require 'fileutils'
7
+ require 'json'
7
8
  require 'yaml'
8
9
  require 'pathname'
9
10
  require 'luca_support/code'
@@ -148,6 +149,26 @@ module LucaRecord # :nodoc:
148
149
  end
149
150
  end
150
151
 
152
+ # update uuid keyed record based on 'id' field.
153
+ # If not found, just create
154
+ #
155
+ def upsert(obj, basedir: @dirname)
156
+ return nil if obj['id'].nil?
157
+
158
+ validate_keys(obj)
159
+ merged = begin
160
+ open_hashed(basedir, obj['id'], 'r') do |f|
161
+ load_data(f).merge(obj)
162
+ end
163
+ rescue
164
+ obj
165
+ end
166
+ open_hashed(basedir, obj['id'], 'w') do |f|
167
+ f.write(YAML.dump(LucaSupport::Code.readable(merged.sort.to_h)))
168
+ end
169
+ obj['id']
170
+ end
171
+
151
172
  # If multiple ID matched, return short ID and human readable label.
152
173
  #
153
174
  def id_completion(phrase, label: 'name', basedir: @dirname)
@@ -333,7 +354,16 @@ module LucaRecord # :nodoc:
333
354
  file_pattern = filename.nil? ? '*' : "#{filename}*"
334
355
  Dir.chdir(abs_path(basedir)) do
335
356
  FileUtils.mkdir_p(subdir) if mode == 'w' && !Dir.exist?(subdir)
336
- Dir.glob("#{subdir}*/#{file_pattern}").sort.each do |subpath|
357
+ records = Dir.glob("#{subdir}*/#{file_pattern}", sort: false)
358
+ records = case records.find_index { |path| FileTest.file?(path) }
359
+ when nil
360
+ # TODO: file_pattern is not valid
361
+ Dir.glob("#{subdir}*/*/#{file_pattern}", sort: false)
362
+ else
363
+ records.sort
364
+ end
365
+ records.each do |subpath|
366
+ next if FileTest.directory?(subpath)
337
367
  next if skip_on_unmatch_code(subpath, code)
338
368
 
339
369
  id_set = subpath.split('/').map { |str| str.split('-') }.flatten
@@ -398,14 +428,16 @@ module LucaRecord # :nodoc:
398
428
  # If specific decode is needed, override this method in each class.
399
429
  #
400
430
  def load_data(io, path = nil)
401
- if @record_type
402
- case @record_type
403
- when 'json'
404
- # TODO: implement JSON parse
405
- end
406
- else
407
- LucaSupport::Code.decimalize(YAML.safe_load(io.read, permitted_classes: [Date])).tap { |obj| validate_keys(obj) }
408
- end
431
+ raw_obj = if @record_type
432
+ case @record_type
433
+ when 'json'
434
+ JSON.parse(io.read)
435
+ end
436
+ else
437
+ YAML.safe_load(io.read, permitted_classes: [Date])
438
+ end
439
+ LucaSupport::Code.decimalize(raw_obj)
440
+ .tap { |obj| validate_keys(obj) }
409
441
  end
410
442
 
411
443
  def validate_keys(obj)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaRecord
4
- VERSION = '0.5.3'
4
+ VERSION = '0.5.5'
5
5
  end
@@ -153,6 +153,7 @@ module LucaSupport # :nodoc:
153
153
  when 'Integer'
154
154
  BigDecimal(obj.to_s)
155
155
  when 'String'
156
+ return obj if /^0[0-9]+$/.match(obj) # zero-prefixed code
156
157
  /^[0-9\.]+$/.match(obj) ? BigDecimal(obj) : obj
157
158
  when 'Float'
158
159
  raise 'already float'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module LucaSupport # :nodoc:
6
+ # Encrypt/Decrypt directory with openssl command
7
+ #
8
+ module Enc
9
+ module_function
10
+
11
+ # TODO: check if openssl/tar exists
12
+ # TODO: handle multiple directories
13
+ # TODO: check space in dir string
14
+ # TODO: check if origin exists
15
+ def encrypt(dir, iter: 10000, cleanup: false)
16
+ Dir.chdir(Pathname(PJDIR) / 'data') do
17
+ system "tar -czf - #{dir} | openssl enc -e -aes256 -iter #{iter} -out #{dir}.tar.gz"
18
+ return if ! cleanup
19
+
20
+ FileUtils.rm_rf dir
21
+ end
22
+ end
23
+
24
+ # TODO: check space in dir string
25
+ def decrypt(dir, iter: 10000)
26
+ Dir.chdir(Pathname(PJDIR) / 'data') do
27
+ system "openssl enc -d -aes256 -iter #{iter} -in #{dir}.tar.gz | tar xz"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'erb'
4
+ require 'json'
4
5
  require 'open3'
5
6
  require 'pathname'
6
7
 
@@ -47,9 +48,16 @@ module LucaSupport
47
48
  nil
48
49
  end
49
50
 
50
- def nushell(yml)
51
+ def nushell(records, columns=[])
52
+ return nil if records.is_a?(String)
53
+
51
54
  require 'open3'
52
- Open3.pipeline_w(%(nu -c 'cat - | from yaml')) { |stdin| stdin.puts yml }
55
+ select = if columns.empty?
56
+ ''
57
+ else
58
+ '| select --ignore-errors ' + columns.map { |col| col.gsub(/[^a-zA-Z0-9_-]/, '') }.join(' ')
59
+ end
60
+ Open3.pipeline_w(%(nu -c 'cat - | from json #{select}')) { |stdin| stdin.puts JSON.dump(records) }
53
61
  end
54
62
  end
55
63
  end
data/lib/luca_support.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  module LucaSupport
4
4
  autoload :Code, 'luca_support/code'
5
+ autoload :Enc, 'luca_support/enc'
5
6
  autoload :CONFIG, 'luca_support/config'
6
7
  autoload :Config, 'luca_support/config'
7
8
  autoload :Mail, 'luca_support/mail'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucarecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-18 00:00:00.000000000 Z
11
+ date: 2023-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - lib/luca_support.rb
72
72
  - lib/luca_support/code.rb
73
73
  - lib/luca_support/config.rb
74
+ - lib/luca_support/enc.rb
74
75
  - lib/luca_support/mail.rb
75
76
  - lib/luca_support/range.rb
76
77
  - lib/luca_support/view.rb
@@ -96,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
97
  - !ruby/object:Gem::Version
97
98
  version: '0'
98
99
  requirements: []
99
- rubygems_version: 3.4.1
100
+ rubygems_version: 3.3.5
100
101
  signing_key:
101
102
  specification_version: 4
102
103
  summary: ERP File operation framework