breezy_template 0.2.0 → 0.3.0
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/lib/breezy_template.rb +285 -211
- data/lib/breezy_template/active_support.rb +3 -3
- data/lib/breezy_template/blank.rb +11 -0
- data/lib/breezy_template/breezy_template.rb +7 -0
- data/lib/breezy_template/cache_extension.rb +75 -27
- data/lib/breezy_template/configuration.rb +3 -1
- data/lib/breezy_template/deferment_extension.rb +14 -12
- data/lib/breezy_template/dependency_tracker.rb +5 -2
- data/lib/breezy_template/digestor.rb +2 -1
- data/lib/breezy_template/errors.rb +24 -0
- data/lib/breezy_template/handler.rb +4 -2
- data/lib/breezy_template/key_formatter.rb +34 -0
- data/lib/breezy_template/partial_extension.rb +57 -19
- data/lib/breezy_template/search_extension.rb +3 -3
- data/test/dependency_tracker_test.rb +80 -64
- data/test/{breezy_template_test.rb → extensions_test.rb} +130 -77
- data/test/template_test.rb +714 -0
- data/test/test_helper.rb +0 -1
- metadata +31 -25
- data/lib/breezy_template/version.rb +0 -3
@@ -7,11 +7,11 @@ if Rails.version >= '4.1'
|
|
7
7
|
alias_method :original_jsonify, :jsonify
|
8
8
|
|
9
9
|
def jsonify(value)
|
10
|
-
if ::BreezyTemplate::
|
10
|
+
if ::BreezyTemplate::Digest === value
|
11
11
|
value
|
12
|
-
elsif ::BreezyTemplate::
|
12
|
+
elsif ::BreezyTemplate::JointVar === value
|
13
13
|
value
|
14
|
-
elsif ::BreezyTemplate::
|
14
|
+
elsif ::BreezyTemplate::DeferVar === value
|
15
15
|
value
|
16
16
|
else
|
17
17
|
original_jsonify(value)
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
|
3
|
+
class BreezyTemplate
|
2
4
|
module CacheExtension
|
3
5
|
class Digest
|
4
6
|
def initialize(digest)
|
@@ -18,44 +20,73 @@ module BreezyTemplate
|
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
def _result(value)
|
22
|
-
|
23
|
-
|
23
|
+
def _result(value, *args)
|
24
|
+
options = _cache_options(args[0])
|
25
|
+
if options
|
26
|
+
_cache(*options) { super }
|
24
27
|
else
|
25
28
|
super
|
26
29
|
end
|
27
30
|
end
|
28
31
|
|
32
|
+
def _normalize_with_cache_options(options)
|
33
|
+
return options unless options.key? :cache
|
34
|
+
|
35
|
+
options = options.dup
|
36
|
+
options[:cache] = [*options[:cache]]
|
37
|
+
|
38
|
+
if options[:cache].size == 1
|
39
|
+
options[:cache].push({})
|
40
|
+
end
|
41
|
+
|
42
|
+
options
|
43
|
+
end
|
44
|
+
|
29
45
|
def set!(key, value = BLANK, *args)
|
30
46
|
options = args.first || {}
|
31
|
-
options =
|
32
|
-
|
33
|
-
|
34
|
-
|
47
|
+
options = _normalize_with_cache_options(options)
|
48
|
+
|
49
|
+
if options[:partial] && options[:cache]
|
50
|
+
partial_name = [*options[:partial]][0]
|
51
|
+
cache_options = options[:cache][1]
|
52
|
+
cache_options[:_partial] = partial_name
|
53
|
+
end
|
54
|
+
|
55
|
+
if ::Kernel.block_given?
|
56
|
+
super(key, value, options, ::Proc.new)
|
57
|
+
else
|
58
|
+
super(key, value, options)
|
35
59
|
end
|
36
|
-
super
|
37
60
|
end
|
38
61
|
|
39
62
|
def array!(collection=[], *attributes)
|
40
|
-
|
41
|
-
|
63
|
+
has_option = attributes.first.is_a? ::Hash
|
64
|
+
return super if !has_option
|
65
|
+
|
66
|
+
options = attributes.first.dup
|
67
|
+
options = _normalize_with_cache_options(options)
|
42
68
|
|
43
|
-
if options[:partial] &&
|
44
|
-
|
45
|
-
|
69
|
+
if options[:partial] && options[:cache]
|
70
|
+
partial_name = [*options[:partial]][0]
|
71
|
+
cache_options = options[:cache][1]
|
72
|
+
cache_options[:_partial] = partial_name
|
46
73
|
end
|
47
74
|
|
48
|
-
|
75
|
+
if ::Kernel.block_given?
|
76
|
+
super(collection, options, ::Proc.new)
|
77
|
+
else
|
78
|
+
super(collection, options)
|
79
|
+
end
|
49
80
|
end
|
50
81
|
|
51
82
|
def _mapping_element(element, opts={})
|
52
|
-
if _cache_options
|
53
|
-
key = _cache_options
|
83
|
+
if _cache_options?(opts)
|
84
|
+
key, options = _cache_options(opts)
|
54
85
|
if ::Proc === key
|
55
86
|
key = key.call(element)
|
56
87
|
end
|
57
88
|
|
58
|
-
_cache(key,
|
89
|
+
_cache(key, options) {
|
59
90
|
_scope { yield element }
|
60
91
|
}
|
61
92
|
else
|
@@ -63,16 +94,20 @@ module BreezyTemplate
|
|
63
94
|
end
|
64
95
|
end
|
65
96
|
|
66
|
-
def _cache_options?
|
67
|
-
|
97
|
+
def _cache_options?(options)
|
98
|
+
!!options[:cache]
|
68
99
|
end
|
69
100
|
|
70
|
-
def _cache_options
|
71
|
-
|
101
|
+
def _cache_options(options)
|
102
|
+
return nil if !options
|
103
|
+
options = [*options[:cache]]
|
104
|
+
key, options = options
|
105
|
+
|
106
|
+
return [key, options]
|
72
107
|
end
|
73
108
|
|
74
109
|
def _extended_options?(value)
|
75
|
-
_cache_options? || super
|
110
|
+
_cache_options?(value) || super
|
76
111
|
end
|
77
112
|
|
78
113
|
def _breezy_set_cache(key, value)
|
@@ -90,7 +125,11 @@ module BreezyTemplate
|
|
90
125
|
end
|
91
126
|
|
92
127
|
def _cache(key=nil, options={})
|
93
|
-
|
128
|
+
perform_caching = @context.respond_to?(:controller) && @context.controller.perform_caching
|
129
|
+
|
130
|
+
if !perform_caching || key.nil?
|
131
|
+
return yield self
|
132
|
+
end
|
94
133
|
|
95
134
|
parent_js = @js
|
96
135
|
key = _cache_key(key, options)
|
@@ -120,11 +159,20 @@ module BreezyTemplate
|
|
120
159
|
end
|
121
160
|
|
122
161
|
def _fragment_name_with_digest(key, options)
|
123
|
-
if
|
124
|
-
partial =
|
162
|
+
if options && options[:_partial]
|
163
|
+
partial = options[:_partial]
|
125
164
|
[key, _partial_digest(partial)]
|
126
165
|
else
|
127
|
-
|
166
|
+
if @context.respond_to?(:cache_fragment_name)
|
167
|
+
# Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
|
168
|
+
# should be used instead.
|
169
|
+
@context.cache_fragment_name(key, options)
|
170
|
+
elsif @context.respond_to?(:fragment_name_with_digest)
|
171
|
+
# Backwards compatibility for period of time when fragment_name_with_digest was made public.
|
172
|
+
@context.fragment_name_with_digest(key)
|
173
|
+
else
|
174
|
+
key
|
175
|
+
end
|
128
176
|
end
|
129
177
|
end
|
130
178
|
end
|
@@ -1,10 +1,13 @@
|
|
1
|
-
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
|
3
|
+
class BreezyTemplate
|
2
4
|
module DefermentExtension
|
3
5
|
ACTIVE_MODES = [:auto, :manual].freeze
|
4
6
|
|
5
7
|
def set!(key, value = BLANK, *args)
|
6
|
-
|
7
|
-
|
8
|
+
options = args[0]
|
9
|
+
if ::Kernel.block_given? && _deferment_options?(options)
|
10
|
+
if _deferment_auto?(options)
|
8
11
|
@js.push(_breezy_visit_current(@path))
|
9
12
|
end
|
10
13
|
return _set_value key, nil
|
@@ -20,17 +23,16 @@ module BreezyTemplate
|
|
20
23
|
"defers.push({url:'#{uri}'});"
|
21
24
|
end
|
22
25
|
|
23
|
-
def _deferment_options?
|
24
|
-
|
25
|
-
|
26
|
+
def _deferment_options?(options)
|
27
|
+
options && !!options[:defer] && (@search_path.nil? || @search_path.size == 0)
|
26
28
|
end
|
27
29
|
|
28
|
-
def _deferment_options
|
29
|
-
|
30
|
+
def _deferment_options(options)
|
31
|
+
options[:defer]
|
30
32
|
end
|
31
33
|
|
32
|
-
def _deferment_auto?
|
33
|
-
_deferment_options
|
34
|
+
def _deferment_auto?(options)
|
35
|
+
_deferment_options(options) == :auto
|
34
36
|
end
|
35
37
|
|
36
38
|
def _set_request_url(request_path)
|
@@ -38,11 +40,11 @@ module BreezyTemplate
|
|
38
40
|
end
|
39
41
|
|
40
42
|
def _extended_options?(value)
|
41
|
-
_deferment_options? || super
|
43
|
+
_deferment_options?(value) || super
|
42
44
|
end
|
43
45
|
|
44
46
|
def _mapping_element(element, options)
|
45
|
-
if _deferment_options?
|
47
|
+
if _deferment_options?(options)
|
46
48
|
if options.has_key? :key
|
47
49
|
id_name = options[:key]
|
48
50
|
id_val = element[id_name]
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# This was taken from jbuilder
|
2
|
+
require 'breezy_template/breezy_template'
|
2
3
|
|
3
4
|
dependency_tracker = false
|
4
5
|
|
@@ -15,7 +16,7 @@ rescue LoadError
|
|
15
16
|
end
|
16
17
|
|
17
18
|
if dependency_tracker
|
18
|
-
class BreezyTemplate
|
19
|
+
class BreezyTemplate
|
19
20
|
module DependencyTrackerMethods
|
20
21
|
# Matches:
|
21
22
|
# json.partial! partial: "comments/comment"
|
@@ -26,6 +27,8 @@ if dependency_tracker
|
|
26
27
|
INDIRECT_RENDERS = /
|
27
28
|
(?::partial\s*=>|partial:) # partial: or :partial =>
|
28
29
|
\s* # optional whitespace
|
30
|
+
\[* # optional Bracket
|
31
|
+
\s* # optional whitespace
|
29
32
|
(['"])([^'"]+)\1 # quoted value
|
30
33
|
/x
|
31
34
|
|
@@ -42,6 +45,6 @@ if dependency_tracker
|
|
42
45
|
end
|
43
46
|
|
44
47
|
::BreezyTemplate::DependencyTracker = Class.new(dependency_tracker::ERBTracker)
|
45
|
-
::BreezyTemplate::DependencyTracker.send :include, ::BreezyTemplate::
|
48
|
+
::BreezyTemplate::DependencyTracker.send :include, ::BreezyTemplate::DependencyTrackerMethods
|
46
49
|
dependency_tracker.register_tracker :breezy, ::BreezyTemplate::DependencyTracker
|
47
50
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
|
3
|
+
class BreezyTemplate
|
4
|
+
class NullError < ::NoMethodError
|
5
|
+
def self.build(key)
|
6
|
+
message = "Failed to add #{key.to_s.inspect} property to null object"
|
7
|
+
new(message)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ArrayError < ::StandardError
|
12
|
+
def self.build(key)
|
13
|
+
message = "Failed to add #{key.to_s.inspect} property to an array"
|
14
|
+
new(message)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class MergeError < ::StandardError
|
19
|
+
def self.build(current_value, updates)
|
20
|
+
message = "Can't merge #{updates.inspect} into #{current_value.inspect}"
|
21
|
+
new(message)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
-
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
|
3
|
+
class BreezyTemplate
|
2
4
|
class Handler
|
3
5
|
cattr_accessor :default_format
|
4
6
|
self.default_format = Mime[:js]
|
5
7
|
|
6
8
|
def self.call(template)
|
7
9
|
# this juggling is required to keep line numbers right in the error
|
8
|
-
%{__already_defined = defined?(json); json||=::BreezyTemplate
|
10
|
+
%{__already_defined = defined?(json); json||=::BreezyTemplate.new(self);json._filter_by_path(breezy_filter) if defined?(breezy_filter); json._set_request_url(request.path);#{template.source}
|
9
11
|
if !(__already_defined && __already_defined != "method")
|
10
12
|
json.merge!({data: json.found! || json.empty! })
|
11
13
|
json.key_format! :downcase
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
require 'active_support/core_ext/array'
|
3
|
+
|
4
|
+
class BreezyTemplate
|
5
|
+
class KeyFormatter
|
6
|
+
def initialize(*args)
|
7
|
+
@format = {}
|
8
|
+
@cache = {}
|
9
|
+
|
10
|
+
options = args.extract_options!
|
11
|
+
args.each do |name|
|
12
|
+
@format[name] = []
|
13
|
+
end
|
14
|
+
options.each do |name, parameters|
|
15
|
+
@format[name] = parameters
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize_copy(original)
|
20
|
+
@cache = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def format(key)
|
24
|
+
@cache[key] ||= @format.inject(key.to_s) do |result, args|
|
25
|
+
func, args = args
|
26
|
+
if ::Proc === func
|
27
|
+
func.call result, *args
|
28
|
+
else
|
29
|
+
result.send func, *args
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
require 'breezy_template/breezy_template'
|
2
|
+
|
3
|
+
class BreezyTemplate
|
2
4
|
module PartialExtension
|
3
5
|
class DeferVar
|
4
6
|
def initialize
|
@@ -37,8 +39,8 @@ module BreezyTemplate
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def set!(key, value = BLANK, *args)
|
40
|
-
options = args.
|
41
|
-
|
42
|
+
options = args.last || {}
|
43
|
+
|
42
44
|
if args.one? && _partial_options?(options)
|
43
45
|
_set_inline_partial key, value, options
|
44
46
|
else
|
@@ -47,11 +49,13 @@ module BreezyTemplate
|
|
47
49
|
end
|
48
50
|
|
49
51
|
def array!(collection = [], *attributes)
|
50
|
-
options = attributes.
|
51
|
-
options =
|
52
|
+
options = attributes.last || {}
|
53
|
+
options = _normalize_options_for_partial(options)
|
52
54
|
|
53
55
|
if attributes.one? && _partial_options?(options)
|
54
|
-
|
56
|
+
_, opts = options[:partial]
|
57
|
+
opts.reverse_merge!(collection: collection)
|
58
|
+
_render_partial_with_options(options)
|
55
59
|
else
|
56
60
|
super
|
57
61
|
end
|
@@ -65,6 +69,20 @@ module BreezyTemplate
|
|
65
69
|
::Hash === options && options.key?(:partial)
|
66
70
|
end
|
67
71
|
|
72
|
+
def _normalize_options_for_partial(options)
|
73
|
+
if !_partial_options?(options)
|
74
|
+
return options
|
75
|
+
end
|
76
|
+
|
77
|
+
partial_options = [*options[:partial]]
|
78
|
+
partial, rest = partial_options
|
79
|
+
if partial && !rest
|
80
|
+
options.dup.merge(partial: [partial, rest || {}])
|
81
|
+
else
|
82
|
+
options
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
68
86
|
def _partial_digest(partial)
|
69
87
|
lookup_context = @context.lookup_context
|
70
88
|
name = lookup_context.find(partial, lookup_context.prefixes, true).virtual_path
|
@@ -72,20 +90,31 @@ module BreezyTemplate
|
|
72
90
|
end
|
73
91
|
|
74
92
|
def _set_inline_partial(name, object, options)
|
75
|
-
|
93
|
+
options = _normalize_options_for_partial(options)
|
94
|
+
|
95
|
+
partial, partial_opts = options[:partial]
|
96
|
+
if partial_opts[:joint] == true
|
97
|
+
partial_opts[:joint] = name
|
98
|
+
end
|
99
|
+
|
100
|
+
value = if object.nil? && partial.empty?
|
76
101
|
[]
|
77
102
|
else
|
78
103
|
locals = {}
|
79
|
-
locals[
|
80
|
-
locals.merge!(
|
104
|
+
locals[partial_opts[:as]] = object if !_blank?(partial_opts) && partial_opts.key?(:as)
|
105
|
+
locals.merge!(partial_opts[:locals]) if partial_opts.key? :locals
|
106
|
+
partial_opts.merge!(locals: locals)
|
81
107
|
|
82
|
-
_scope{ _render_partial
|
108
|
+
_scope{ _render_partial(options) }
|
83
109
|
end
|
84
110
|
|
85
|
-
|
111
|
+
options = options.dup
|
112
|
+
options.delete(:partial)
|
113
|
+
set! name, value, options
|
86
114
|
end
|
87
115
|
|
88
116
|
def _render_partial(options)
|
117
|
+
partial, options = options[:partial]
|
89
118
|
joint = options[:joint]
|
90
119
|
if joint
|
91
120
|
joint = joint.to_sym
|
@@ -95,25 +124,34 @@ module BreezyTemplate
|
|
95
124
|
end
|
96
125
|
|
97
126
|
options[:locals].merge! json: self
|
98
|
-
@context.render options
|
127
|
+
@context.render options.merge(partial: partial)
|
99
128
|
end
|
100
129
|
|
101
130
|
def _render_partial_with_options(options)
|
131
|
+
options = _normalize_options_for_partial(options)
|
132
|
+
partial, partial_opts = options[:partial]
|
102
133
|
ary_opts = options.dup
|
103
|
-
options.reverse_merge! locals: {}
|
104
|
-
options.reverse_merge! ::BreezyTemplate::Template.template_lookup_options
|
105
|
-
as = options[:as]
|
106
134
|
|
107
|
-
|
108
|
-
|
109
|
-
|
135
|
+
partial_opts.reverse_merge! locals: {}
|
136
|
+
partial_opts.reverse_merge! ::BreezyTemplate.template_lookup_options
|
137
|
+
as = partial_opts[:as]
|
138
|
+
|
139
|
+
if partial_opts.key?(:collection)
|
140
|
+
collection = partial_opts.delete(:collection)
|
141
|
+
extract_joint_name = partial_opts.delete(:joint)
|
142
|
+
locals = partial_opts.delete(:locals)
|
110
143
|
|
111
144
|
ary_opts.delete(:partial)
|
112
145
|
array! collection, ary_opts do |member|
|
146
|
+
|
113
147
|
member_locals = locals.clone
|
114
148
|
member_locals.merge! collection: collection
|
115
149
|
member_locals.merge! as.to_sym => member if as
|
116
|
-
|
150
|
+
partial_opts.merge!(locals: member_locals)
|
151
|
+
if extract_joint_name.respond_to?(:call)
|
152
|
+
partial_opts.merge!(joint: extract_joint_name.call(member))
|
153
|
+
end
|
154
|
+
_render_partial options
|
117
155
|
end
|
118
156
|
else
|
119
157
|
_render_partial options
|