lb-persistence 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/lib/lb/persistence.rb +1 -0
- data/lib/lb/persistence/functions.rb +75 -0
- data/lib/lb/persistence/version.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/lb/persistence/functions/class_methods/group_prefix_spec.rb +57 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6711a6b664d00404fcfa651e207552cc579fa18b8c81058bdcb0e0c92c1bc19
|
4
|
+
data.tar.gz: 900ef6dc07b7c17efe819392d74569af7ce813831ca6d8ec44df46e218abff50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0736a5eec2ab6002fd28944f9baf02669d90f0176474f5bfad24ad470fc26d21f75d2d7f6d4ed1b04cd996205017392b0d296a62957f0a62dc523dbd91935233
|
7
|
+
data.tar.gz: 59b80e5a19985c579cbc5abc1b5a4f630f5bda9af3c69fa20a09a27767615d2ff51658d657c8065c4d6954cac994e7168fdfe7e52b752008db1dbe9da18f2a90
|
data/config/flay.yml
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
---
|
2
|
-
threshold:
|
3
|
-
total_score:
|
2
|
+
threshold: 9
|
3
|
+
total_score: 65
|
data/config/flog.yml
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
---
|
2
|
-
threshold:
|
2
|
+
threshold: 13.7
|
data/lib/lb/persistence.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LB
|
4
|
+
class Persistence
|
5
|
+
# LB transproc functions
|
6
|
+
module Functions
|
7
|
+
extend Transproc::Registry
|
8
|
+
extend Transproc::Composer
|
9
|
+
|
10
|
+
import Transproc::Coercions
|
11
|
+
import Transproc::Conditional
|
12
|
+
import Transproc::ArrayTransformations
|
13
|
+
import Transproc::HashTransformations
|
14
|
+
import Transproc::ClassTransformations
|
15
|
+
import Transproc::ProcTransformations
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def debug_p(value)
|
19
|
+
p value
|
20
|
+
end
|
21
|
+
|
22
|
+
def debug_pp(value)
|
23
|
+
pp value
|
24
|
+
end
|
25
|
+
|
26
|
+
def debug_puts(value)
|
27
|
+
puts value
|
28
|
+
end
|
29
|
+
|
30
|
+
def remove_prefix(key, prefix)
|
31
|
+
remove_prefix_from_key(key.to_s, prefix).to_sym
|
32
|
+
end
|
33
|
+
|
34
|
+
def remove_prefix_from_key(key, prefix)
|
35
|
+
key.start_with?(prefix) ? key.gsub(/^#{prefix}/, '') : key
|
36
|
+
end
|
37
|
+
|
38
|
+
def group_prefix(array, key, keys, prefix, model = nil)
|
39
|
+
compose do |ops|
|
40
|
+
ops << t(:group, key, keys)
|
41
|
+
ops << t(:map_array,
|
42
|
+
t(:remove_key_prefix_inject_for, key, prefix, model))
|
43
|
+
end.call(array)
|
44
|
+
end
|
45
|
+
|
46
|
+
def remove_key_prefix_inject_for(hash, key, prefix, model = nil)
|
47
|
+
t(:map_value, key,
|
48
|
+
t(:map_array,
|
49
|
+
t(:remove_key_prefix_inject, prefix, model))).call(hash)
|
50
|
+
end
|
51
|
+
|
52
|
+
def remove_key_prefix_inject(keys, prefix, model = nil)
|
53
|
+
compose do |ops|
|
54
|
+
ops << t(:remove_key_prefix, prefix)
|
55
|
+
ops << t(:inject_if_given, model)
|
56
|
+
end.call(keys)
|
57
|
+
end
|
58
|
+
|
59
|
+
def remove_key_prefix(keys, prefix)
|
60
|
+
t(:map_keys, t(:remove_prefix, prefix)).call(keys)
|
61
|
+
end
|
62
|
+
|
63
|
+
def inject_if_given(value, model)
|
64
|
+
t(:guard, ->(_v) { !model.nil? },
|
65
|
+
t(:constructor_inject, model)).call(value)
|
66
|
+
end
|
67
|
+
|
68
|
+
def inject_array(array, model)
|
69
|
+
t(:map_array, t(:constructor_inject, model)).call(array)
|
70
|
+
end
|
71
|
+
alias model inject_array
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe LB::Persistence::Functions, '.group_prefix' do
|
6
|
+
subject { object.call(data) }
|
7
|
+
|
8
|
+
let(:object) do
|
9
|
+
described_class.t(:group_prefix, key, keys, prefix, nested_class)
|
10
|
+
.>> described_class.t(:model, model_class)
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:key) { :items }
|
14
|
+
let(:keys) { [:item_a] }
|
15
|
+
let(:prefix) { 'item_' }
|
16
|
+
|
17
|
+
let(:model_class) do
|
18
|
+
nested = nested_class
|
19
|
+
Class.new(Dry::Struct) do
|
20
|
+
module Types
|
21
|
+
include Dry::Types.module
|
22
|
+
end
|
23
|
+
|
24
|
+
attribute :a, Types::Strict::String
|
25
|
+
attribute :items, Types::Strict::Array.member(nested)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
let(:nested_class) do
|
30
|
+
Class.new(Dry::Struct) do
|
31
|
+
module Types
|
32
|
+
include Dry::Types.module
|
33
|
+
end
|
34
|
+
|
35
|
+
attribute :a, Types::Strict::String
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
let(:data) do
|
40
|
+
[
|
41
|
+
{ a: 'a1', item_a: 'item_a1' },
|
42
|
+
{ a: 'a1', item_a: 'item_a2' },
|
43
|
+
{ a: 'a2', item_a: 'item_a3' }
|
44
|
+
]
|
45
|
+
end
|
46
|
+
|
47
|
+
let(:expected_hash_values) do
|
48
|
+
[
|
49
|
+
{ a: 'a1', items: [{ a: 'item_a1' }, { a: 'item_a2' }] },
|
50
|
+
{ a: 'a2', items: [{ a: 'item_a3' }] }
|
51
|
+
]
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should group hash' do
|
55
|
+
expect(subject.map(&:to_h)).to eq(expected_hash_values)
|
56
|
+
end
|
57
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lb-persistence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Firas Zaidan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-struct
|
@@ -189,6 +189,7 @@ files:
|
|
189
189
|
- lb-persistence.gemspec
|
190
190
|
- lib/lb-persistence.rb
|
191
191
|
- lib/lb/persistence.rb
|
192
|
+
- lib/lb/persistence/functions.rb
|
192
193
|
- lib/lb/persistence/model/attributes.rb
|
193
194
|
- lib/lb/persistence/relation/joins.rb
|
194
195
|
- lib/lb/persistence/settings.rb
|
@@ -201,6 +202,7 @@ files:
|
|
201
202
|
- spec/unit/lb/persistence/class_methods/new_spec.rb
|
202
203
|
- spec/unit/lb/persistence/connect_spec.rb
|
203
204
|
- spec/unit/lb/persistence/container_spec.rb
|
205
|
+
- spec/unit/lb/persistence/functions/class_methods/group_prefix_spec.rb
|
204
206
|
- spec/unit/lb/persistence/model/redefine_attribute_spec.rb
|
205
207
|
- spec/unit/lb/persistence/repository_spec.rb
|
206
208
|
- spec/unit/lb/persistence/uri_spec.rb
|
@@ -236,6 +238,7 @@ test_files:
|
|
236
238
|
- spec/unit/lb/persistence/class_methods/new_spec.rb
|
237
239
|
- spec/unit/lb/persistence/connect_spec.rb
|
238
240
|
- spec/unit/lb/persistence/container_spec.rb
|
241
|
+
- spec/unit/lb/persistence/functions/class_methods/group_prefix_spec.rb
|
239
242
|
- spec/unit/lb/persistence/model/redefine_attribute_spec.rb
|
240
243
|
- spec/unit/lb/persistence/repository_spec.rb
|
241
244
|
- spec/unit/lb/persistence/uri_spec.rb
|