lucarecord 0.2.15 → 0.2.16

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: 3d7ac838f340b86b71208f40357ebbe7c4217b1ed6b69dcf012b773d8fa0a532
4
- data.tar.gz: c0e706fe9e1eef4abbb5d9600a5100931cd9384d45459481142fb3c594dbfd32
3
+ metadata.gz: 222229f2cc5299302f8ff1df81a92ce9a507101e6035765cdaa1408ad8e93365
4
+ data.tar.gz: 30102479db3e25fca972cf723d8f679891a179ed0b9ceb79cb03d2b22f25743b
5
5
  SHA512:
6
- metadata.gz: 56f2f5575afb0a5e204a94ed66fa7b6ded3831316e9d71be17a02270f464b57551701c3938a447f479a4512255800d49bdc545e8f970f8d81f00c1be8caf5316
7
- data.tar.gz: be5bf831649fb6d6ad74ca44ae18c9cbb4eabf26193926979776fb99f843c687a8dead90917dafeaa3e9289c2ec8eb83e78a2082fad50305df1f4b8bcaab96a9
6
+ metadata.gz: 4be9918878c1d9204ccbaac61cfefac3872b028d192e8013d177fda369092dcd1ff2884e7e0e53a1384e3e9b21f57cbca903b6f448bddb39ec2199dd4aefdf46
7
+ data.tar.gz: b5730ce678fc0259a4550f14fddce5655509d07653c0fd8d405d9d1f0f3e48572cad1818790e29ead2d23144be95a3b9229aaf424bca4b71b61c12603b16c0fc
@@ -29,7 +29,7 @@ module LucaRecord # :nodoc:
29
29
 
30
30
  # find ID based record. Support uuid and encoded date.
31
31
  def find(id, basedir = @dirname)
32
- return enum_for(:find, id, basedir) unless block_given?
32
+ return enum_for(:find, id, basedir).first unless block_given?
33
33
 
34
34
  if id.length >= 40
35
35
  open_hashed(basedir, id) do |f|
@@ -91,6 +91,7 @@ module LucaRecord # :nodoc:
91
91
 
92
92
  # create hash based record
93
93
  def create(obj, basedir = @dirname)
94
+ validate_keys(obj)
94
95
  id = LucaSupport::Code.issue_random_id
95
96
  obj['id'] = id
96
97
  open_hashed(basedir, id, 'w') do |f|
@@ -121,6 +122,25 @@ module LucaRecord # :nodoc:
121
122
  File.write(path, YAML.dump(origin.sort.to_h))
122
123
  end
123
124
 
125
+ # update file with obj['id']
126
+ def save(obj, basedir = @dirname)
127
+ if obj['id'].nil?
128
+ create(obj, basedir)
129
+ else
130
+ validate_keys(obj)
131
+ open_hashed(basedir, obj['id'], 'w') do |f|
132
+ f.write(YAML.dump(obj.sort.to_h))
133
+ end
134
+ end
135
+ obj['id']
136
+ end
137
+
138
+ # delete file by id
139
+ def delete(id, basedir = @dirname)
140
+ FileUtils.rm(Pathname(abs_path(basedir)) / id2path(id))
141
+ id
142
+ end
143
+
124
144
  # ----------------------------------------------------------------
125
145
  # :section: Path Utilities
126
146
  # ----------------------------------------------------------------
@@ -234,7 +254,17 @@ module LucaRecord # :nodoc:
234
254
  when 'json'
235
255
  # TODO: implement JSON parse
236
256
  else
237
- YAML.load(io.read)
257
+ YAML.load(io.read).tap { |obj| validate_keys(obj) }
258
+ end
259
+ end
260
+
261
+ def validate_keys(obj)
262
+ return nil unless @required
263
+
264
+ keys = obj.keys
265
+ [].tap do |errors|
266
+ @required.each { |r| errors << r unless keys.include?(r) }
267
+ raise "Missing keys: #{errors.join(' ')}" unless errors.empty?
238
268
  end
239
269
  end
240
270
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LucaRecord
4
- VERSION = '0.2.15'
4
+ VERSION = '0.2.16'
5
5
  end
@@ -97,7 +97,7 @@ module LucaSupport
97
97
  #
98
98
  def take_current(dat, item)
99
99
  target = dat.dig(item)
100
- if target.class.name == 'Array' && target.map(&:keys).flatten.include?('effective')
100
+ if target.is_a?(Array) && target.map(&:keys).flatten.include?('effective')
101
101
  latest = target
102
102
  .filter { |a| Date.parse(a.dig('effective').to_s) < @date }
103
103
  .max { |a, b| Date.parse(a.dig('effective').to_s) <=> Date.parse(b.dig('effective').to_s) }
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.2.15
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail