lucarecord 0.5.1 → 0.5.4

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: a08f1701d74d851bed03a7715a3732cdd7250c7ea819785965a7bfbb612d5ded
4
- data.tar.gz: a0859eae9aeef68a80eac597c5f2fe3b1cf3f72c285b22451b0312df99f98d98
3
+ metadata.gz: d7a1835b0493134002d92d7a9b8f8fb6a55305f470636c448da8848f9b6afdaa
4
+ data.tar.gz: 9ec15acf15451babe4280c098e670f68a377e9be9af8e312f3b0b558476da995
5
5
  SHA512:
6
- metadata.gz: 40b5cebc5335ca1d7f67f47c73b5964614fb576e5acd3788d82cabd35df8552552584e5d6a218841c3ea69109bb950879d039e94801126c2f6b789c81fde646a
7
- data.tar.gz: fc64a3de02ddc1b61f8806999ddc20fff4199ee836a0a9b612f0172f710ebb4ebe976424f1a22b136438074797a6cf96bb5b2372499ac5cb29f80123c50d4091
6
+ metadata.gz: 7741483aa8e7e6ca1fb01325f546e703b4a530d48b7d68823f127402c39a092d01ee1bcc6a2f319794bd0830a7095957405b833f02c11e480fec6df399f37997
7
+ data.tar.gz: 90934508dd586dc6582be9366b22d9a60958ce247c0d971d36207016b5ec5b69ee7375f6613cc7e37fe920fd135c2fd85dd5482458763e9372b7ae0c39bf78e7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## LucaRecord 0.5.4
2
+
3
+ * add `upsert()`
4
+ * `open_records()` supports git-like id sub directories
5
+ * Regard zero-prefixed code like '00123' as not BigDecimal but String on YAML load
6
+ * Add support table columns order to `LucaSupport::View.nushell()`
7
+ * Breaking change: `LucaSupport::View.nushell()` takes Ruby Array. YAML is not accepted anymore.
8
+
9
+ ## LucaRecord 0.5.3
10
+
11
+ * add `find_secure()` for partial data encryption.
12
+
13
+ ## LucaRecord 0.5.2
14
+
15
+ * add `update_record()` by `open_records()` id_set.
16
+
1
17
  ## LucaRecord 0.5.1
2
18
 
3
19
  * add `--enable-local-file-access` option to `wkhtmltopdf` command.
@@ -46,6 +46,37 @@ module LucaRecord # :nodoc:
46
46
  end
47
47
  end
48
48
 
49
+ # Merge 2 files under plain & prefixed dirs.
50
+ # prefixed contents will override duplicated keys.
51
+ # This function does not provide encryption/decryption.
52
+ #
53
+ def find_secure(id, basedir = @dirname, prefix = 's_')
54
+ if id.length >= 40
55
+ plain = open_hashed(basedir, id) do |f|
56
+ load_data(f)
57
+ end
58
+ secure = begin
59
+ open_hashed("#{prefix}#{basedir}", id) do |f|
60
+ load_data(f)
61
+ end
62
+ rescue
63
+ # No file exists, and so on.
64
+ {}
65
+ end
66
+ elsif id.length >= 7
67
+ parts = id.split('/')
68
+ plain = open_records(basedir, parts[0], parts[1]) do |f|
69
+ load_data(f)
70
+ end
71
+ secure = open_records("#{prefix}#{basedir}", parts[0], parts[1]) do |f|
72
+ load_data(f)
73
+ end
74
+ else
75
+ raise 'specified id length is too short'
76
+ end
77
+ plain.merge(secure)
78
+ end
79
+
49
80
  # search date based record.
50
81
  #
51
82
  # * data hash
@@ -117,6 +148,26 @@ module LucaRecord # :nodoc:
117
148
  end
118
149
  end
119
150
 
151
+ # update uuid keyed record based on 'id' field.
152
+ # If not found, just create
153
+ #
154
+ def upsert(obj, basedir: @dirname)
155
+ return nil if obj['id'].nil?
156
+
157
+ validate_keys(obj)
158
+ merged = begin
159
+ open_hashed(basedir, obj['id'], 'r') do |f|
160
+ load_data(f).merge(obj)
161
+ end
162
+ rescue
163
+ obj
164
+ end
165
+ open_hashed(basedir, obj['id'], 'w') do |f|
166
+ f.write(YAML.dump(LucaSupport::Code.readable(merged.sort.to_h)))
167
+ end
168
+ obj['id']
169
+ end
170
+
120
171
  # If multiple ID matched, return short ID and human readable label.
121
172
  #
122
173
  def id_completion(phrase, label: 'name', basedir: @dirname)
@@ -302,7 +353,16 @@ module LucaRecord # :nodoc:
302
353
  file_pattern = filename.nil? ? '*' : "#{filename}*"
303
354
  Dir.chdir(abs_path(basedir)) do
304
355
  FileUtils.mkdir_p(subdir) if mode == 'w' && !Dir.exist?(subdir)
305
- Dir.glob("#{subdir}*/#{file_pattern}").sort.each do |subpath|
356
+ records = Dir.glob("#{subdir}*/#{file_pattern}", sort: false)
357
+ records = case records.find_index { |path| FileTest.file?(path) }
358
+ when nil
359
+ # TODO: file_pattern is not valid
360
+ Dir.glob("#{subdir}*/*/#{file_pattern}", sort: false)
361
+ else
362
+ records.sort
363
+ end
364
+ records.each do |subpath|
365
+ next if FileTest.directory?(subpath)
306
366
  next if skip_on_unmatch_code(subpath, code)
307
367
 
308
368
  id_set = subpath.split('/').map { |str| str.split('-') }.flatten
@@ -311,6 +371,17 @@ module LucaRecord # :nodoc:
311
371
  end
312
372
  end
313
373
 
374
+ # Write record with id_set supplied by open_records().
375
+ #
376
+ def update_record(basedir, id_set, content)
377
+ raise "path fragments are too short" if id_set.length < 2
378
+
379
+ ::IO.write(
380
+ abs_path(basedir) / id_set[0] / id_set[1..-1].join('-'),
381
+ content
382
+ )
383
+ end
384
+
314
385
  # Calculate md5sum with original digest, file content and filename(optional).
315
386
  #
316
387
  def update_digest(digest, str, filename = nil)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaRecord
4
- VERSION = '0.5.1'
4
+ VERSION = '0.5.4'
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.1
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-05-26 00:00:00.000000000 Z
11
+ date: 2023-04-11 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
@@ -81,7 +82,7 @@ metadata:
81
82
  homepage_uri: https://github.com/chumaltd/luca/tree/master/lucarecord
82
83
  source_code_uri: https://github.com/chumaltd/luca/tree/master/lucarecord
83
84
  changelog_uri: https://github.com/chumaltd/luca/tree/master/lucarecord/CHANGELOG.md
84
- post_install_message:
85
+ post_install_message:
85
86
  rdoc_options: []
86
87
  require_paths:
87
88
  - lib
@@ -97,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
98
  version: '0'
98
99
  requirements: []
99
100
  rubygems_version: 3.3.5
100
- signing_key:
101
+ signing_key:
101
102
  specification_version: 4
102
103
  summary: ERP File operation framework
103
104
  test_files: []