tzispa_rig 0.5.2 → 0.5.3

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
  SHA1:
3
- metadata.gz: fa5e5300f16c59b1d36a54cb61457d7f27d73db7
4
- data.tar.gz: 6253800420d94b93692dee73102c8accdd9cafe5
3
+ metadata.gz: 83f9244aa0671284489e35cc3ab1d69313acb7cc
4
+ data.tar.gz: 78644d54bf7fc9ee38447c8f88c95cfb60dc23aa
5
5
  SHA512:
6
- metadata.gz: 21d4d0c0e55ffea5ef20a6cef84fb5e47e14ac8af437c87e6bdeef7fb042108c449dd21c8b765c988345c3af10066b242a3147aaac8dccf02dce5e1840a1fb13
7
- data.tar.gz: dbc9b3e8095d7115fd33889892dd8963acf644db99394b72108953120a4f870d41c10eee72372e5a8fdd59c19e67638b2b3d817d3b1aa7b63e9a49796555b336
6
+ metadata.gz: 5f97e685d30424bc8a34084d75b9ccb5daa01f4ad75bba8158991d2a5f656cc410654d357b845753fbcdbf3c5badbd6bed924bedb43974903c48201d6d992082
7
+ data.tar.gz: a46ee803a956f44b75b3d6b841ca82abf957e7ac1ce7bea4b521b444f5496bed8d13508cbcb486b0d6670e20fe9c0412d881681a89e8efd3b0decf6f2e4dfa7a
data/CHANGELOG.md CHANGED
@@ -2,6 +2,9 @@ Tzispa Rig
2
2
 
3
3
  Rig templates implementation
4
4
 
5
+ @@ v0.5.3
6
+ - added before/after hooks in template binders
7
+
5
8
  ## v0.5.2
6
9
  - replace lru_redux with moneta to improve template cache storage
7
10
  - added fallback to super in method_missing at LoopBinder
@@ -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
 
7
5
  module Tzispa
8
6
  module Rig
@@ -19,12 +17,6 @@ module Tzispa
19
17
  end
20
18
  end
21
19
 
22
- class IsnotTemplateBinder < ArgumentError
23
- def initialize(name)
24
- super "Class #{name} isn't a TemplateBinder"
25
- end
26
- end
27
-
28
20
  class Binder
29
21
  extend Forwardable
30
22
 
@@ -66,10 +58,8 @@ module Tzispa
66
58
  # loop_id<Symbol>: The id of the template loop to bind
67
59
  #
68
60
  def loop_binder(loop_id)
69
- loop_parser = @parser.loop_parser loop_id
70
- unknown? loop_parser
71
- duplicated? loop_parser
72
- LoopBinder.new loop_parser[0], @context
61
+ lp = loop_parser?(loop_id)
62
+ LoopBinder.new(lp.first, @context) if lp
73
63
  end
74
64
 
75
65
  def attr_cache(*attrs)
@@ -78,63 +68,14 @@ module Tzispa
78
68
 
79
69
  private
80
70
 
81
- def unknown?(loop_parser)
82
- raise UnknownTag.new(self.class.name, loop_parser.id) unless loop_parser.count.positive?
71
+ def loop_parser?(loop_id)
72
+ lp = @parser.loop_parser loop_id
73
+ duplicated? lp
74
+ lp if lp.count.positive?
83
75
  end
84
76
 
85
77
  def duplicated?(loop_parser)
86
- raise DuplicatedLoop.new(self.class.name, loop_parser.id) unless loop_parser.count == 1
87
- end
88
- end
89
-
90
- class TemplateBinder < Binder
91
- extend Forwardable
92
-
93
- using Tzispa::Utils::TzHash
94
-
95
- attr_reader :template
96
- def_delegators :@template, :params
97
-
98
- def initialize(template, context)
99
- super(template.parser, context)
100
- @template = template
101
- end
102
-
103
- def data
104
- @data ||= @data_struct.new
105
- end
106
-
107
- def attach(json = nil, **params)
108
- attach_json(json) if json
109
- data.tap do |d|
110
- params.each do |k, v|
111
- raise UnknownTag.new(self.class.name, k) unless tags.include? k
112
- d[k] = v.is_a?(Enumerable) ? attach_loop(k, v) : v
113
- end
114
- end
115
- end
116
-
117
- def attach_json(data)
118
- src = JSON.parse(data).symbolize!
119
- attach(src.reject { |_, v| v.is_a?(Array) || v.is_a?(::Hash) })
120
- attach(src.select { |_, v| v.is_a?(Array) }.map do |k, v|
121
- [k, attach_loop(k, v)]
122
- end.to_h)
123
- end
124
-
125
- def attach_loop(k, v)
126
- loop_binder(k).bind! { v.map { |item| loop_item item.symbolize! } }
127
- end
128
-
129
- def self.for(template, context)
130
- if template.bindable?
131
- binder_class = template.binder_class
132
- binder_class&.new(template, context)&.tap do |binder|
133
- raise IsnotTemplateBinder(binder_class.name) unless binder.is_a? TemplateBinder
134
- end
135
- else
136
- new(template, context)
137
- end
78
+ raise DuplicatedLoop.new(self.class.name, loop_parser.id) if loop_parser.count > 1
138
79
  end
139
80
  end
140
81
 
@@ -162,7 +103,7 @@ module Tzispa
162
103
  def loop_item(params = nil)
163
104
  (LoopItem.new self).tap do |item|
164
105
  params&.each do |k, v|
165
- raise UnknownTag(self.class.name, k) unless tags.include? k
106
+ next unless tags.include? k
166
107
  item.data[k] = v
167
108
  end
168
109
  end
@@ -6,7 +6,7 @@ require 'tzispa/utils/string'
6
6
  require 'tzispa/utils/indenter'
7
7
  require 'tzispa/rig/parameters'
8
8
  require 'tzispa/rig/parsernext'
9
- require 'tzispa/rig/binder'
9
+ require 'tzispa/rig/template_binder'
10
10
 
11
11
  module Tzispa
12
12
  module Rig
@@ -97,7 +97,7 @@ module Tzispa
97
97
 
98
98
  using Tzispa::Utils::TzString
99
99
 
100
- BASIC_TYPES = [:layout, :block, :static].freeze
100
+ BASIC_TYPES = %i[layout block static].freeze
101
101
  RIG_EXTENSION = 'rig'
102
102
 
103
103
  attr_reader :id, :name, :type, :domain, :parser, :subdomain, :childrens, :content_type
@@ -133,7 +133,7 @@ module Tzispa
133
133
  def render(context, binder = nil)
134
134
  parse! unless parser
135
135
  binder ||= TemplateBinder.for self, context
136
- binder.bind! if binder&.respond_to?(:bind!)
136
+ binder.bound
137
137
  parser.render binder
138
138
  end
139
139
 
@@ -205,7 +205,7 @@ module Tzispa
205
205
  end
206
206
 
207
207
  def create_binder
208
- return unless [:block, :layout].include?(type)
208
+ return unless %i[block layout].include?(type)
209
209
  ::File.open("#{domain.path}/#{binder_require}.rb", 'w') do |f|
210
210
  f.puts write_binder_code
211
211
  end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'json'
5
+ require 'tzispa/utils/hash'
6
+ require 'tzispa/helpers/hooks/before'
7
+ require 'tzispa/helpers/hooks/after'
8
+ require 'tzispa/rig/binder'
9
+
10
+ module Tzispa
11
+ module Rig
12
+
13
+ class IsnotTemplateBinder < ArgumentError
14
+ def initialize(name)
15
+ super "Class #{name} isn't a TemplateBinder"
16
+ end
17
+ end
18
+
19
+ class TemplateBinder < Binder
20
+ extend Forwardable
21
+
22
+ using Tzispa::Utils::TzHash
23
+
24
+ include Tzispa::Helpers::Hooks::Before
25
+ include Tzispa::Helpers::Hooks::After
26
+
27
+ attr_reader :template
28
+ def_delegators :@template, :params
29
+
30
+ def initialize(template, context)
31
+ super(template.parser, context)
32
+ @template = template
33
+ end
34
+
35
+ def bound
36
+ do_before
37
+ bind! if respond_to?(:bind!)
38
+ do_after
39
+ end
40
+
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
+ def self.for(template, context)
75
+ if template.bindable?
76
+ binder_class = template.binder_class
77
+ binder_class&.new(template, context)&.tap do |binder|
78
+ raise IsnotTemplateBinder(binder_class.name) unless binder.is_a? TemplateBinder
79
+ end
80
+ else
81
+ new(template, context)
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+ end
@@ -3,7 +3,7 @@
3
3
  module Tzispa
4
4
  module Rig
5
5
 
6
- VERSION = '0.5.2'
6
+ VERSION = '0.5.3'
7
7
  GEM_NAME = 'tzispa_rig'
8
8
 
9
9
  end
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.2
4
+ version: 0.5.3
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-21 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tzispa_utils
@@ -69,6 +69,7 @@ files:
69
69
  - lib/tzispa/rig/parsernext.rb
70
70
  - lib/tzispa/rig/syntax.rb
71
71
  - lib/tzispa/rig/template.rb
72
+ - lib/tzispa/rig/template_binder.rb
72
73
  - lib/tzispa/rig/token.rb
73
74
  - lib/tzispa/rig/type_token/api_url.rb
74
75
  - lib/tzispa/rig/type_token/block.rb