breezy_template 0.5.4 → 0.5.5
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 +13 -28
- data/lib/breezy_template/core_ext.rb +20 -0
- data/lib/breezy_template/errors.rb +7 -0
- data/lib/breezy_template/handler.rb +1 -2
- data/lib/breezy_template/search_extension.rb +13 -25
- data/test/extensions_test.rb +27 -121
- data/test/template_test.rb +18 -99
- data/test/test_helper.rb +13 -2
- metadata +3 -3
- data/lib/breezy_template/key_formatter.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64f7958f565f036dc57e27e520c791e7b16e5f0d
|
4
|
+
data.tar.gz: 5d1ef989831b31f354b3d1e578a6be2cc757da3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef5697134625758c22427708e99e5b8eb707b2af94414b52c98b88a3ebd98c493f8f8756306135f0df3a80ae1fc029ee2006ebccd484394538eb9ca7c6076dff
|
7
|
+
data.tar.gz: 939b5591aa0d9c6062228708e893d8cfa5be07252d50a280401d7044cb9df382fe20108c9fdfb65821cee1295b554e6b31d251eb9e35b31b604d1fcf89a1aa43
|
data/lib/breezy_template.rb
CHANGED
@@ -2,7 +2,6 @@ require 'breezy_template/breezy_template'
|
|
2
2
|
|
3
3
|
require 'breezy_template/blank'
|
4
4
|
require 'breezy_template/var'
|
5
|
-
require 'breezy_template/key_formatter'
|
6
5
|
require 'breezy_template/errors'
|
7
6
|
|
8
7
|
require 'breezy_template/active_support'
|
@@ -34,7 +33,6 @@ class BreezyTemplate
|
|
34
33
|
|
35
34
|
self.template_lookup_options = { handlers: [:breezy, :props] }
|
36
35
|
|
37
|
-
@@key_formatter = nil
|
38
36
|
@@ignore_nil = false
|
39
37
|
|
40
38
|
def initialize(context, options = {})
|
@@ -44,7 +42,6 @@ class BreezyTemplate
|
|
44
42
|
@joints = {}
|
45
43
|
|
46
44
|
@attributes = {}
|
47
|
-
@key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil}
|
48
45
|
@ignore_nil = options.fetch(:ignore_nil, @@ignore_nil)
|
49
46
|
|
50
47
|
yield self if ::Kernel.block_given?
|
@@ -106,15 +103,6 @@ class BreezyTemplate
|
|
106
103
|
@path.pop
|
107
104
|
end
|
108
105
|
|
109
|
-
def key_format!(*args)
|
110
|
-
@key_formatter = KeyFormatter.new(*args)
|
111
|
-
end
|
112
|
-
|
113
|
-
# Same as the instance method key_format! except sets the default.
|
114
|
-
def self.key_format(*args)
|
115
|
-
@@key_formatter = KeyFormatter.new(*args)
|
116
|
-
end
|
117
|
-
|
118
106
|
def empty!
|
119
107
|
attributes = @attributes
|
120
108
|
@attributes = {}
|
@@ -135,24 +123,21 @@ class BreezyTemplate
|
|
135
123
|
@attributes << _scope{ yield self }
|
136
124
|
end
|
137
125
|
|
138
|
-
|
139
|
-
def array!(collection = [], *attributes)
|
126
|
+
def array!(collection, *attributes)
|
140
127
|
options = attributes.first || {}
|
141
128
|
|
142
|
-
|
129
|
+
if !collection.respond_to? :member_by
|
130
|
+
raise ::NotImplementedError, 'collection must implement member_by(attr, value)'
|
131
|
+
end
|
132
|
+
|
133
|
+
if !collection.respond_to? :member_at
|
134
|
+
raise ::NotImplementedError, 'collection must implement member_at(index)'
|
135
|
+
end
|
136
|
+
|
137
|
+
|
143
138
|
collection = _prepare_collection_for_map(collection)
|
144
139
|
array = if ::Kernel.block_given?
|
145
140
|
_map_collection(collection, options, &::Proc.new)
|
146
|
-
# elsif attributes.any?
|
147
|
-
# if (!attributes.last.is_a? ::Hash)
|
148
|
-
# _map_collection(collection) { |element|
|
149
|
-
# extract! element, *attributes
|
150
|
-
# }
|
151
|
-
# else
|
152
|
-
# _map_collection(collection, options) { |element|
|
153
|
-
# extract! element, *attributes
|
154
|
-
# }
|
155
|
-
# end
|
156
141
|
else
|
157
142
|
collection.to_a
|
158
143
|
end
|
@@ -226,7 +211,7 @@ class BreezyTemplate
|
|
226
211
|
end
|
227
212
|
|
228
213
|
def _key(key)
|
229
|
-
|
214
|
+
key.to_s
|
230
215
|
end
|
231
216
|
|
232
217
|
def _set_value(key, value)
|
@@ -264,12 +249,12 @@ class BreezyTemplate
|
|
264
249
|
end
|
265
250
|
|
266
251
|
def _scope
|
267
|
-
parent_attributes
|
252
|
+
parent_attributes = @attributes
|
268
253
|
@attributes = BLANK
|
269
254
|
yield
|
270
255
|
@attributes
|
271
256
|
ensure
|
272
|
-
@attributes
|
257
|
+
@attributes = parent_attributes
|
273
258
|
end
|
274
259
|
|
275
260
|
def _is_collection?(object)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class BreezyTemplate
|
2
|
+
module Extensions
|
3
|
+
module Array
|
4
|
+
module Member
|
5
|
+
def member_at(index)
|
6
|
+
at(index)
|
7
|
+
end
|
8
|
+
|
9
|
+
def member_by(attribute, value)
|
10
|
+
raise NotImplementedError, 'Implement member_by(attr, value) in your own delegate'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Array
|
18
|
+
include BreezyTemplate::Extensions::Array::Member
|
19
|
+
end
|
20
|
+
|
@@ -9,8 +9,7 @@ class BreezyTemplate
|
|
9
9
|
# this juggling is required to keep line numbers right in the error
|
10
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}
|
11
11
|
if !(__already_defined && __already_defined != "method")
|
12
|
-
json.merge!({data: json.
|
13
|
-
json.key_format! :downcase
|
12
|
+
json.merge!({data: json._found! || json.empty! })
|
14
13
|
if defined?(breezy) && breezy
|
15
14
|
breezy.each do |k, v|
|
16
15
|
json.set! k, v
|
@@ -2,10 +2,14 @@ require 'breezy_template/breezy_template'
|
|
2
2
|
|
3
3
|
class BreezyTemplate
|
4
4
|
module SearchExtension
|
5
|
-
def
|
5
|
+
def _found!
|
6
|
+
if !@search_path.nil? && @found.nil?
|
7
|
+
::Kernel.raise NotFoundError.build(@search_path)
|
8
|
+
end
|
9
|
+
|
6
10
|
found = @found
|
7
11
|
@found = nil
|
8
|
-
@search_path =
|
12
|
+
@search_path = nil
|
9
13
|
found
|
10
14
|
end
|
11
15
|
|
@@ -36,31 +40,15 @@ class BreezyTemplate
|
|
36
40
|
if @search_path && !@search_path.empty?
|
37
41
|
id_name, id_val = @search_path.first.split('=')
|
38
42
|
|
39
|
-
if
|
40
|
-
|
41
|
-
|
42
|
-
collection = collection.where(::Hash[id_name, id_val])
|
43
|
-
else
|
44
|
-
index = id_name.to_i
|
45
|
-
collection = collection.offset(index).limit(1)
|
46
|
-
end
|
43
|
+
if id_val
|
44
|
+
id_val = id_val.to_i
|
45
|
+
found = collection.member_by(id_name, id_val)
|
47
46
|
else
|
48
|
-
|
49
|
-
|
50
|
-
found = collection.find do |ele|
|
51
|
-
ele[id_name] == id_val || ele[id_name.to_sym] == id_val
|
52
|
-
end
|
53
|
-
else
|
54
|
-
index = id_name.to_i
|
55
|
-
found = collection[index]
|
56
|
-
end
|
57
|
-
|
58
|
-
if found
|
59
|
-
collection = [found]
|
60
|
-
else
|
61
|
-
collection = []
|
62
|
-
end
|
47
|
+
index = id_name.to_i
|
48
|
+
found = collection.member_at(index)
|
63
49
|
end
|
50
|
+
|
51
|
+
found ? [found] : []
|
64
52
|
else
|
65
53
|
super
|
66
54
|
end
|
data/test/extensions_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
require "mocha"
|
3
|
-
|
3
|
+
require 'delegate'
|
4
4
|
require "action_view"
|
5
5
|
require "action_view/testing/resolvers"
|
6
6
|
require "breezy_template"
|
@@ -106,7 +106,9 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def action_controller_test_request
|
109
|
-
if ::Rails.version.start_with?('5.
|
109
|
+
if ::Rails.version.start_with?('5.2')
|
110
|
+
::ActionController::TestRequest.create({})
|
111
|
+
elsif ::Rails.version.start_with?('5.1')
|
110
112
|
::ActionController::TestRequest.create({})
|
111
113
|
elsif ::Rails.version.start_with?('5')
|
112
114
|
::ActionController::TestRequest.create
|
@@ -263,48 +265,6 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
263
265
|
assert_equal expected, result
|
264
266
|
end
|
265
267
|
|
266
|
-
test "key_format! with parameter" do
|
267
|
-
result = jbuild(<<-JBUILDER)
|
268
|
-
json.key_format! camelize: [:lower]
|
269
|
-
json.camel_style "for JS"
|
270
|
-
JBUILDER
|
271
|
-
|
272
|
-
expected = strip_format(<<-JS)
|
273
|
-
(function(){
|
274
|
-
var joints={};
|
275
|
-
var cache={};
|
276
|
-
var defers=[];
|
277
|
-
return ({"data":{"camelStyle":"for JS"},"joints":joints,"defers":defers});
|
278
|
-
})()
|
279
|
-
JS
|
280
|
-
|
281
|
-
assert_equal expected, result
|
282
|
-
end
|
283
|
-
|
284
|
-
test "key_format! propagates to child elements" do
|
285
|
-
result = jbuild(<<-JBUILDER)
|
286
|
-
json.key_format! :upcase
|
287
|
-
json.level1 "one"
|
288
|
-
json.level2 do
|
289
|
-
json.value "two"
|
290
|
-
end
|
291
|
-
JBUILDER
|
292
|
-
|
293
|
-
expected = strip_format(<<-JS)
|
294
|
-
(function(){
|
295
|
-
var joints={};
|
296
|
-
var cache={};
|
297
|
-
var defers=[];
|
298
|
-
return ({"data":{
|
299
|
-
"LEVEL1":"one",
|
300
|
-
"LEVEL2":{"VALUE":"two"}
|
301
|
-
},"joints":joints,"defers":defers});
|
302
|
-
})()
|
303
|
-
JS
|
304
|
-
|
305
|
-
assert_equal expected, result
|
306
|
-
end
|
307
|
-
|
308
268
|
test "renders partial via the option through set!" do
|
309
269
|
@post = BLOG_POST_COLLECTION.first
|
310
270
|
Rails.cache.clear
|
@@ -517,9 +477,9 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
517
477
|
assert_equal expected, result
|
518
478
|
end
|
519
479
|
|
520
|
-
test "renders array of partials as empty array with
|
480
|
+
test "renders array of partials as empty array with an empty collection" do
|
521
481
|
result = jbuild(<<-JBUILDER)
|
522
|
-
json.array!
|
482
|
+
json.array! [], partial: ["blog_post", as: :blog_post]
|
523
483
|
JBUILDER
|
524
484
|
|
525
485
|
expected = strip_format(<<-JS)
|
@@ -935,6 +895,23 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
935
895
|
assert_equal expected, result
|
936
896
|
end
|
937
897
|
|
898
|
+
test "filtering for a nonexistant node in the tree" do
|
899
|
+
begin
|
900
|
+
jbuild(<<-JBUILDER)
|
901
|
+
json._filter_by_path('miss.miss.miss.miss')
|
902
|
+
json.hit do
|
903
|
+
json.hit2 do
|
904
|
+
json.greeting 'hello world'
|
905
|
+
end
|
906
|
+
end
|
907
|
+
JBUILDER
|
908
|
+
rescue => e
|
909
|
+
assert_equal e.cause.class, BreezyTemplate::NotFoundError
|
910
|
+
assert_equal e.message, 'Could not find node at ["miss", "miss", "miss", "miss"]'
|
911
|
+
end
|
912
|
+
|
913
|
+
Rails.cache.clear
|
914
|
+
end
|
938
915
|
test "filtering for a raw value is also possble" do
|
939
916
|
result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
|
940
917
|
json.hit do
|
@@ -1041,84 +1018,12 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
1041
1018
|
assert_equal expected, result
|
1042
1019
|
end
|
1043
1020
|
|
1044
|
-
test "filtering for a node of a AR relation in a tree by id via an appended where clause" do
|
1045
|
-
Post.delete_all
|
1046
|
-
Note.delete_all
|
1047
|
-
|
1048
|
-
post = Post.create
|
1049
|
-
first_note = post.notes.create(title: 'first')
|
1050
|
-
post.notes.create(title: 'second')
|
1051
|
-
|
1052
|
-
result = jbuild(<<-JBUILDER, breezy_filter: "hit.hit2.id=#{first_note.id}")
|
1053
|
-
post = Post.first
|
1054
|
-
first_note = Note.where(title: 'first').first
|
1055
|
-
post.notes.expects(:where).once().with('id'=>first_note.id).returns([{id: first_note.id, title: 'first'}])
|
1056
|
-
|
1057
|
-
json.hit do
|
1058
|
-
json.hit2 do
|
1059
|
-
json.array! post.notes do |x|
|
1060
|
-
raise 'this should be be called' if x[:title] == 'second'
|
1061
|
-
json.title x[:title]
|
1062
|
-
end
|
1063
|
-
end
|
1064
|
-
end
|
1065
|
-
JBUILDER
|
1066
|
-
|
1067
|
-
Rails.cache.clear
|
1068
|
-
id = Note.where(title: 'first').first.id
|
1069
|
-
expected = strip_format(<<-JS)
|
1070
|
-
(function(){
|
1071
|
-
var joints={};
|
1072
|
-
var cache={};
|
1073
|
-
var defers=[];
|
1074
|
-
return (
|
1075
|
-
{"data":{"title":"first"},"action":"graft","path":"hit.hit2.id=#{id}","joints":joints,"defers":defers}
|
1076
|
-
);
|
1077
|
-
})()
|
1078
|
-
JS
|
1079
|
-
assert_equal expected, result
|
1080
|
-
end
|
1081
|
-
|
1082
|
-
|
1083
|
-
test "filtering for a node of a AR relation in a tree by index via an appended where clause" do
|
1084
|
-
result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.0')
|
1085
|
-
post = Post.create
|
1086
|
-
post.notes.create title: 'first'
|
1087
|
-
post.notes.create title: 'second'
|
1088
|
-
|
1089
|
-
offset = post.notes.offset(0)
|
1090
|
-
post.notes.expects(:offset).once().with(0).returns(offset)
|
1091
|
-
|
1092
|
-
json.hit do
|
1093
|
-
json.hit2 do
|
1094
|
-
json.array! post.notes do |x|
|
1095
|
-
raise 'this should be be called' if x[:title] == 'second'
|
1096
|
-
json.title x[:title]
|
1097
|
-
end
|
1098
|
-
end
|
1099
|
-
end
|
1100
|
-
JBUILDER
|
1101
|
-
|
1102
|
-
Rails.cache.clear
|
1103
|
-
|
1104
|
-
expected = strip_format(<<-JS)
|
1105
|
-
(function(){
|
1106
|
-
var joints={};
|
1107
|
-
var cache={};
|
1108
|
-
var defers=[];
|
1109
|
-
return (
|
1110
|
-
{"data":{"title":"first"},"action":"graft","path":"hit.hit2.0","joints":joints,"defers":defers}
|
1111
|
-
);
|
1112
|
-
})()
|
1113
|
-
JS
|
1114
|
-
assert_equal expected, result
|
1115
|
-
end
|
1116
|
-
|
1117
1021
|
test "filtering for a node in an array of a tree by id" do
|
1118
1022
|
result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.id=1')
|
1119
1023
|
json.hit do
|
1120
1024
|
json.hit2 do
|
1121
|
-
|
1025
|
+
data = ObjectCollection.new([{id: 1, name: 'hit' }, {id:2, name: 'miss'}])
|
1026
|
+
json.array! data do |x|
|
1122
1027
|
raise 'this should be be called' if x[:name] == 'miss'
|
1123
1028
|
json.name x[:name]
|
1124
1029
|
end
|
@@ -1143,9 +1048,10 @@ class BreezyTemplateTest < ActionView::TestCase
|
|
1143
1048
|
|
1144
1049
|
test "filtering for a node in an array of a tree by index" do
|
1145
1050
|
result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.0')
|
1051
|
+
data = [{id: 1, name: 'hit' }, {id:2, name: 'miss'}]
|
1146
1052
|
json.hit do
|
1147
1053
|
json.hit2 do
|
1148
|
-
json.array!
|
1054
|
+
json.array! data do |x|
|
1149
1055
|
raise 'this should be be called' if x[:name] == 'miss'
|
1150
1056
|
json.name x[:name]
|
1151
1057
|
end
|
data/test/template_test.rb
CHANGED
@@ -15,13 +15,29 @@ class NonEnumerable
|
|
15
15
|
end
|
16
16
|
|
17
17
|
delegate :map, :count, to: :@collection
|
18
|
+
|
19
|
+
def member_at(index)
|
20
|
+
@collection[index]
|
21
|
+
end
|
22
|
+
|
23
|
+
def member_by(attribute, value)
|
24
|
+
raise NotImplementedError
|
25
|
+
end
|
18
26
|
end
|
19
27
|
|
20
|
-
class VeryBasicWrapper
|
28
|
+
class VeryBasicWrapper
|
21
29
|
def initialize(thing)
|
22
30
|
@thing = thing
|
23
31
|
end
|
24
32
|
|
33
|
+
def member_at(index)
|
34
|
+
@thing[index]
|
35
|
+
end
|
36
|
+
|
37
|
+
def member_by(attribute, value)
|
38
|
+
raise NotImplementedError
|
39
|
+
end
|
40
|
+
|
25
41
|
def method_missing(name, *args, &block)
|
26
42
|
@thing.send name, *args, &block
|
27
43
|
end
|
@@ -50,10 +66,6 @@ end
|
|
50
66
|
|
51
67
|
|
52
68
|
class TemplateTest < ActiveSupport::TestCase
|
53
|
-
setup do
|
54
|
-
BreezyTemplate.send :class_variable_set, '@@key_formatter', nil
|
55
|
-
end
|
56
|
-
|
57
69
|
test 'single key' do
|
58
70
|
result = jbuild do |json|
|
59
71
|
json.content 'hello'
|
@@ -260,7 +272,7 @@ class TemplateTest < ActiveSupport::TestCase
|
|
260
272
|
# assert_equal [], result['comments']
|
261
273
|
# end
|
262
274
|
|
263
|
-
test 'nesting multiple children from a non-Enumerable that responds to #map' do
|
275
|
+
test 'nesting multiple children from a non-Enumerable that responds to #map, #members_at, #members_by' do
|
264
276
|
comments = NonEnumerable.new([ Comment.new('hello', 1), Comment.new('world', 2) ])
|
265
277
|
|
266
278
|
result = jbuild do |json|
|
@@ -292,7 +304,6 @@ class TemplateTest < ActiveSupport::TestCase
|
|
292
304
|
|
293
305
|
test 'array! casts array-like objects to array before merging' do
|
294
306
|
wrapped_array = VeryBasicWrapper.new(%w[foo bar])
|
295
|
-
|
296
307
|
result = jbuild do |json|
|
297
308
|
json.array! wrapped_array
|
298
309
|
end
|
@@ -481,98 +492,6 @@ class TemplateTest < ActiveSupport::TestCase
|
|
481
492
|
# assert_equal 50, result['relations'][1]['age']
|
482
493
|
# end
|
483
494
|
|
484
|
-
# reconsider
|
485
|
-
# test 'initialize via options hash' do
|
486
|
-
# jbuilder = Jbuilder.new(key_formatter: 1, ignore_nil: 2)
|
487
|
-
# assert_equal 1, jbuilder.instance_eval{ @key_formatter }
|
488
|
-
# assert_equal 2, jbuilder.instance_eval{ @ignore_nil }
|
489
|
-
# end
|
490
|
-
|
491
|
-
test 'key_format! with parameter' do
|
492
|
-
result = jbuild do |json|
|
493
|
-
json.key_format! camelize: [:lower]
|
494
|
-
json.camel_style 'for JS'
|
495
|
-
end
|
496
|
-
|
497
|
-
assert_equal ['camelStyle'], result.keys
|
498
|
-
end
|
499
|
-
|
500
|
-
test 'key_format! with parameter not as an array' do
|
501
|
-
result = jbuild do |json|
|
502
|
-
json.key_format! :camelize => :lower
|
503
|
-
json.camel_style 'for JS'
|
504
|
-
end
|
505
|
-
|
506
|
-
assert_equal ['camelStyle'], result.keys
|
507
|
-
end
|
508
|
-
|
509
|
-
test 'key_format! propagates to child elements' do
|
510
|
-
result = jbuild do |json|
|
511
|
-
json.key_format! :upcase
|
512
|
-
json.level1 'one'
|
513
|
-
json.level2 do
|
514
|
-
json.value 'two'
|
515
|
-
end
|
516
|
-
end
|
517
|
-
|
518
|
-
assert_equal 'one', result['LEVEL1']
|
519
|
-
assert_equal 'two', result['LEVEL2']['VALUE']
|
520
|
-
end
|
521
|
-
|
522
|
-
test 'key_format! resets after child element' do
|
523
|
-
result = jbuild do |json|
|
524
|
-
json.level2 do
|
525
|
-
json.key_format! :upcase
|
526
|
-
json.value 'two'
|
527
|
-
end
|
528
|
-
json.level1 'one'
|
529
|
-
end
|
530
|
-
|
531
|
-
assert_equal 'two', result['level2']['VALUE']
|
532
|
-
assert_equal 'one', result['level1']
|
533
|
-
end
|
534
|
-
|
535
|
-
test 'key_format! with no parameter' do
|
536
|
-
result = jbuild do |json|
|
537
|
-
json.key_format! :upcase
|
538
|
-
json.lower 'Value'
|
539
|
-
end
|
540
|
-
|
541
|
-
assert_equal ['LOWER'], result.keys
|
542
|
-
end
|
543
|
-
|
544
|
-
test 'key_format! with multiple steps' do
|
545
|
-
result = jbuild do |json|
|
546
|
-
json.key_format! :upcase, :pluralize
|
547
|
-
json.pill 'foo'
|
548
|
-
end
|
549
|
-
|
550
|
-
assert_equal ['PILLs'], result.keys
|
551
|
-
end
|
552
|
-
|
553
|
-
test 'key_format! with lambda/proc' do
|
554
|
-
result = jbuild do |json|
|
555
|
-
json.key_format! ->(key){ key + ' and friends' }
|
556
|
-
json.oats 'foo'
|
557
|
-
end
|
558
|
-
|
559
|
-
assert_equal ['oats and friends'], result.keys
|
560
|
-
end
|
561
|
-
|
562
|
-
test 'default key_format!' do
|
563
|
-
BreezyTemplate.key_format camelize: :lower
|
564
|
-
result = jbuild{ |json| json.camel_style 'for JS' }
|
565
|
-
assert_equal ['camelStyle'], result.keys
|
566
|
-
end
|
567
|
-
#
|
568
|
-
test 'do not use default key formatter directly' do
|
569
|
-
BreezyTemplate.key_format
|
570
|
-
jbuild{ |json| json.key 'value' }
|
571
|
-
formatter = BreezyTemplate.send(:class_variable_get, '@@key_formatter')
|
572
|
-
cache = formatter.instance_variable_get('@cache')
|
573
|
-
assert_empty cache
|
574
|
-
end
|
575
|
-
|
576
495
|
test 'ignore_nil! without a parameter' do
|
577
496
|
result = jbuild do |json|
|
578
497
|
json.ignore_nil!
|
data/test/test_helper.rb
CHANGED
@@ -11,6 +11,17 @@ ActiveSupport::TestCase.test_order = :random if ActiveSupport::TestCase.respond_
|
|
11
11
|
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
|
12
12
|
Rails.cache = ActiveSupport::Cache::MemoryStore.new
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
class ObjectCollection < SimpleDelegator
|
15
|
+
def member_at(index)
|
16
|
+
at(index)
|
17
|
+
end
|
16
18
|
|
19
|
+
def member_by(key, val)
|
20
|
+
find do |ele|
|
21
|
+
ele[key] == val || ele[key.to_sym] == val
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
require 'breezy_template/core_ext'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: breezy_template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johny Ho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -92,12 +92,12 @@ files:
|
|
92
92
|
- lib/breezy_template/breezy_template.rb
|
93
93
|
- lib/breezy_template/cache_extension.rb
|
94
94
|
- lib/breezy_template/configuration.rb
|
95
|
+
- lib/breezy_template/core_ext.rb
|
95
96
|
- lib/breezy_template/deferment_extension.rb
|
96
97
|
- lib/breezy_template/dependency_tracker.rb
|
97
98
|
- lib/breezy_template/digestor.rb
|
98
99
|
- lib/breezy_template/errors.rb
|
99
100
|
- lib/breezy_template/handler.rb
|
100
|
-
- lib/breezy_template/key_formatter.rb
|
101
101
|
- lib/breezy_template/partial_extension.rb
|
102
102
|
- lib/breezy_template/search_extension.rb
|
103
103
|
- lib/breezy_template/var.rb
|
@@ -1,34 +0,0 @@
|
|
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
|