tzispa_rig 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -1
- data/lib/tzispa/rig/binder.rb +38 -6
- data/lib/tzispa/rig/template_binder.rb +1 -38
- data/lib/tzispa/rig/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e9b0801d5dd6d70a7e9787b110ea8ddcd481058b
|
4
|
+
data.tar.gz: fc1e6464c849608c5818cdbd10323de2377cc597
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5968f3ffc90ead75ffdaa7ea700aeea5089e4efe2f311d8c3c035f25b01ced2b3d86eb67f8d20c2f0dd3d4fae8a7adce7a974b28e7fb7acbe1d53ee7efff60c
|
7
|
+
data.tar.gz: e8c1965acb288ead946bba1d0d90a3f2319a3931747d9563dc2eaaa15d79dbf4c95ec05a0bc1c36f2cbac915e7c73f52ee0c40ec0dd64b66199beab0fcdc6024
|
data/CHANGELOG.md
CHANGED
data/lib/tzispa/rig/binder.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
|
+
require 'json'
|
5
|
+
require 'tzispa/utils/hash'
|
4
6
|
|
5
7
|
module Tzispa
|
6
8
|
module Rig
|
@@ -58,8 +60,8 @@ module Tzispa
|
|
58
60
|
# loop_id<Symbol>: The id of the template loop to bind
|
59
61
|
#
|
60
62
|
def loop_binder(loop_id)
|
61
|
-
|
62
|
-
LoopBinder.new(
|
63
|
+
prl = loop_parser?(loop_id)
|
64
|
+
LoopBinder.new(prl, context) if prl
|
63
65
|
end
|
64
66
|
|
65
67
|
def attr_cache(*attrs)
|
@@ -71,7 +73,7 @@ module Tzispa
|
|
71
73
|
def loop_parser?(loop_id)
|
72
74
|
lp = @parser.loop_parser loop_id
|
73
75
|
duplicated? lp
|
74
|
-
lp if lp.count.positive?
|
76
|
+
lp.first if lp.count.positive?
|
75
77
|
end
|
76
78
|
|
77
79
|
def duplicated?(loop_parser)
|
@@ -79,9 +81,39 @@ module Tzispa
|
|
79
81
|
end
|
80
82
|
end
|
81
83
|
|
82
|
-
class
|
83
|
-
|
84
|
+
class AttachBinder < Binder
|
85
|
+
using Tzispa::Utils::TzHash
|
84
86
|
|
87
|
+
def data
|
88
|
+
@data ||= data_struct.new
|
89
|
+
end
|
90
|
+
|
91
|
+
def attach(**params)
|
92
|
+
data.tap do |d|
|
93
|
+
params.each do |k, v|
|
94
|
+
next unless tags.include? k
|
95
|
+
d[k] = evaluate k, v
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def attach_json(data)
|
101
|
+
attach(JSON.parse(data).deep_symbolize!)
|
102
|
+
end
|
103
|
+
|
104
|
+
def evaluate(k, value)
|
105
|
+
case value
|
106
|
+
when Enumerable
|
107
|
+
loop_binder(k)&.bind! { value.reject(&:nil?).map { |item| loop_item item } } || value
|
108
|
+
when Proc
|
109
|
+
loop_binder(k)&.bind!(&value) || value.call
|
110
|
+
else
|
111
|
+
value
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class LoopBinder < AttachBinder
|
85
117
|
def bind!(&generator)
|
86
118
|
@source_object = eval 'self', generator.binding
|
87
119
|
@data = instance_eval(&generator).to_enum(:each)
|
@@ -104,7 +136,7 @@ module Tzispa
|
|
104
136
|
(LoopItem.new self).tap do |item|
|
105
137
|
params&.each do |k, v|
|
106
138
|
next unless tags.include? k
|
107
|
-
item.data[k] = v
|
139
|
+
item.data[k] = evaluate k, v
|
108
140
|
end
|
109
141
|
end
|
110
142
|
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
|
-
require 'json'
|
5
|
-
require 'tzispa/utils/hash'
|
6
4
|
require 'tzispa/helpers/hooks/before'
|
7
5
|
require 'tzispa/helpers/hooks/after'
|
8
6
|
require 'tzispa/rig/binder'
|
@@ -16,11 +14,9 @@ module Tzispa
|
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
|
-
class TemplateBinder <
|
17
|
+
class TemplateBinder < AttachBinder
|
20
18
|
extend Forwardable
|
21
19
|
|
22
|
-
using Tzispa::Utils::TzHash
|
23
|
-
|
24
20
|
include Tzispa::Helpers::Hooks::Before
|
25
21
|
include Tzispa::Helpers::Hooks::After
|
26
22
|
|
@@ -38,39 +34,6 @@ module Tzispa
|
|
38
34
|
do_after
|
39
35
|
end
|
40
36
|
|
41
|
-
def data
|
42
|
-
@data ||= @data_struct.new
|
43
|
-
end
|
44
|
-
|
45
|
-
def attach(json = nil, **params)
|
46
|
-
attach_json(json) if json
|
47
|
-
data.tap do |d|
|
48
|
-
params.each do |k, v|
|
49
|
-
next unless tags.include? k
|
50
|
-
d[k] = case v
|
51
|
-
when Enumerable
|
52
|
-
bind_loopitems(k, v)
|
53
|
-
when Proc
|
54
|
-
loop_binder(k)&.bind!(&v) || v.call
|
55
|
-
else
|
56
|
-
v
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def attach_json(data)
|
63
|
-
src = JSON.parse(data).symbolize!
|
64
|
-
attach(src.reject { |_, v| v.is_a?(Array) || v.is_a?(::Hash) })
|
65
|
-
attach(src.select { |_, v| v.is_a?(Array) }.map do |k, v|
|
66
|
-
[k, bind_loopitems(k, v)]
|
67
|
-
end.to_h)
|
68
|
-
end
|
69
|
-
|
70
|
-
def bind_loopitems(k, v)
|
71
|
-
loop_binder(k)&.bind! { v.map { |item| loop_item item.symbolize! } }
|
72
|
-
end
|
73
|
-
|
74
37
|
def self.for(template, context)
|
75
38
|
if template.bindable?
|
76
39
|
binder_class = template.binder_class
|
data/lib/tzispa/rig/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tzispa_rig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Antonio Piñero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tzispa_utils
|