rabl 0.14.0 → 0.14.1
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 +5 -5
- data/CHANGELOG.md +4 -2
- data/CONTRIBUTING.md +2 -2
- data/Gemfile +1 -0
- data/lib/rabl/builder.rb +16 -14
- data/lib/rabl/cache_engine.rb +30 -1
- data/lib/rabl/engine.rb +15 -8
- data/lib/rabl/helpers.rb +7 -3
- data/lib/rabl/version.rb +1 -1
- data/test/silence.rb +10 -8
- data/test/teststrap.rb +6 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3278a66ba6ca1b6bbbba630f618972311efa3ffa
|
4
|
+
data.tar.gz: 8b01a9837edb00add1cc9ba51a4fbd81c5b0cd15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b7e34e9c1f6b7ee7f5660147805671bf6ee54252be2199cd7cecf5f78ce4b3ff876884fe072bb2b1633288542e5e9cc8db79990b139086bb718b2ee4eb483da
|
7
|
+
data.tar.gz: 735b6dc4e773a9ea3613aada27e2ee57afe987a5defd8c5260962ac690191b05fce69722765793d6858083910b685d4b9cde8eaaa5aaadbbd91cae235f15f995
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
## 0.14.
|
3
|
+
## 0.14.1 (April 28th, 2019)
|
4
4
|
|
5
|
-
*
|
5
|
+
* Many adjustments and various non-breaking fixes
|
6
|
+
* Improvements to caching (@zenspider)
|
7
|
+
* Fix tests for Rails 5 (@robertoz)
|
6
8
|
|
7
9
|
## 0.13.1 (October 21st, 2016)
|
8
10
|
|
data/CONTRIBUTING.md
CHANGED
@@ -3,7 +3,7 @@ We love pull requests. Here's a quick guide:
|
|
3
3
|
1. Fork the repo.
|
4
4
|
|
5
5
|
2. Run the tests. We only take pull requests with passing tests, and it's great
|
6
|
-
to know that you have a clean slate: `bundle && rake test`
|
6
|
+
to know that you have a clean slate: `bundle && bundle exec rake test`
|
7
7
|
|
8
8
|
3. Add a test for your change. Only refactoring and documentation changes
|
9
9
|
require no new tests. If you are adding functionality or fixing a bug, we need
|
@@ -34,4 +34,4 @@ Syntax:
|
|
34
34
|
|
35
35
|
And in case we didn't emphasize it enough: we love tests!
|
36
36
|
|
37
|
-
NOTE: Adapted from https://raw.github.com/thoughtbot/factory_girl_rails/master/CONTRIBUTING.md
|
37
|
+
NOTE: Adapted from https://raw.github.com/thoughtbot/factory_girl_rails/master/CONTRIBUTING.md
|
data/Gemfile
CHANGED
data/lib/rabl/builder.rb
CHANGED
@@ -137,7 +137,10 @@ module Rabl
|
|
137
137
|
# attribute :foo, :as => "bar"
|
138
138
|
# attribute :foo, :as => "bar", :if => lambda { |m| m.foo }
|
139
139
|
def attribute(name, options = {})
|
140
|
-
return unless
|
140
|
+
return unless
|
141
|
+
@_object &&
|
142
|
+
attribute_present?(name) &&
|
143
|
+
resolve_condition(options)
|
141
144
|
|
142
145
|
attribute = data_object_attribute(name)
|
143
146
|
name = create_key(options[:as] || name)
|
@@ -200,10 +203,11 @@ module Rabl
|
|
200
203
|
|
201
204
|
# Evaluate conditions given a symbol/proc/lambda/variable to evaluate
|
202
205
|
def call_condition_proc(condition, object)
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
condition
|
206
|
+
case condition
|
207
|
+
when Proc then condition.call(object)
|
208
|
+
when Symbol then condition.to_proc.call(object)
|
209
|
+
else condition
|
210
|
+
end
|
207
211
|
end
|
208
212
|
|
209
213
|
# resolve_condition(:if => true) => true
|
@@ -213,8 +217,10 @@ module Rabl
|
|
213
217
|
# resolve_condition(:unless => lambda { |m| false }, :if => proc { true}) => true
|
214
218
|
def resolve_condition(options)
|
215
219
|
result = true
|
216
|
-
result &&=
|
217
|
-
|
220
|
+
result &&= call_condition_proc(options[:if], @_object) if
|
221
|
+
options.key?(:if)
|
222
|
+
result &&= !call_condition_proc(options[:unless], @_object) if
|
223
|
+
options.key?(:unless)
|
218
224
|
result
|
219
225
|
end
|
220
226
|
|
@@ -222,13 +228,9 @@ module Rabl
|
|
222
228
|
# Checks if an attribute is present. If not, check if the configuration specifies that this is an error
|
223
229
|
# attribute_present?(created_at) => true
|
224
230
|
def attribute_present?(name)
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
raise "Failed to render missing attribute #{name}"
|
229
|
-
else
|
230
|
-
false
|
231
|
-
end
|
231
|
+
@_object.respond_to?(name) ||
|
232
|
+
(Rabl.configuration.raise_on_missing_attribute &&
|
233
|
+
raise("Failed to render missing attribute #{name}"))
|
232
234
|
end
|
233
235
|
|
234
236
|
# Returns a guess at the format in this context_scope
|
data/lib/rabl/cache_engine.rb
CHANGED
@@ -5,8 +5,35 @@
|
|
5
5
|
# config.cache_engine = AdvancedCacheEngine.new
|
6
6
|
#
|
7
7
|
|
8
|
+
class LRU < Hash
|
9
|
+
attr_accessor :max_size
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super
|
13
|
+
self.max_size = 100_000
|
14
|
+
end
|
15
|
+
|
16
|
+
def []= k,v
|
17
|
+
r = super
|
18
|
+
limit_size
|
19
|
+
r
|
20
|
+
end
|
21
|
+
|
22
|
+
def limit_size
|
23
|
+
if size > max_size then
|
24
|
+
delete keys.shift while size > max_size
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
8
29
|
module Rabl
|
9
30
|
class CacheEngine
|
31
|
+
def initialize
|
32
|
+
unless defined?(Rails)
|
33
|
+
@cache = LRU.new
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
10
37
|
|
11
38
|
# Fetch given a key and options and a fallback block attempts to find the key in the cache
|
12
39
|
# and stores the block result in there if no key is found.
|
@@ -17,13 +44,15 @@ module Rabl
|
|
17
44
|
if defined?(Rails)
|
18
45
|
Rails.cache.fetch(key, cache_options, &block)
|
19
46
|
else
|
20
|
-
yield
|
47
|
+
@cache[key] ||= yield
|
21
48
|
end
|
22
49
|
end
|
23
50
|
|
24
51
|
def write(key, value, options = {})
|
25
52
|
if defined?(Rails)
|
26
53
|
Rails.cache.write(key, value, options)
|
54
|
+
else
|
55
|
+
@cache[key] = yield
|
27
56
|
end
|
28
57
|
end
|
29
58
|
|
data/lib/rabl/engine.rb
CHANGED
@@ -60,7 +60,7 @@ module Rabl
|
|
60
60
|
if digestor_available? && respond_to?(:lookup_context) && lookup_context
|
61
61
|
template = @_options[:template] || @virtual_path
|
62
62
|
|
63
|
-
digest =
|
63
|
+
digest =
|
64
64
|
if Gem::Version.new(Rails.version) >= Gem::Version.new('4.1')
|
65
65
|
Digestor.digest(:name => template, :finder => lookup_context)
|
66
66
|
else
|
@@ -83,7 +83,7 @@ module Rabl
|
|
83
83
|
|
84
84
|
options[:root_name] = determine_object_root(data, root_name, options[:root])
|
85
85
|
|
86
|
-
result =
|
86
|
+
result =
|
87
87
|
if is_object?(data) || !data # object @user
|
88
88
|
Builder.new(data, @_settings, options).to_hash
|
89
89
|
elsif is_collection?(data) # collection @users
|
@@ -167,6 +167,7 @@ module Rabl
|
|
167
167
|
@_root_name_data = @_root_name_data.values.first if @_root_name_data.is_a?(Hash)
|
168
168
|
|
169
169
|
# If we turn this around, `@_root_name_date ==` may trigger data to be loaded unnecessarily.
|
170
|
+
# TODO: is nil a different semantic? otherwise don't use `false ==`, use !
|
170
171
|
if false == @_root_name_data
|
171
172
|
@_object_root_name = false
|
172
173
|
@_collection_name = false
|
@@ -409,15 +410,21 @@ module Rabl
|
|
409
410
|
end
|
410
411
|
|
411
412
|
def eval_source(locals, &block)
|
412
|
-
# Note: locals and block may be used by the eval'ed source
|
413
|
-
|
414
413
|
return unless @_source.present?
|
415
414
|
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
415
|
+
msg = "cached_source_#{@_source.hash.abs}"
|
416
|
+
|
417
|
+
unless self.respond_to? msg then
|
418
|
+
src = "def #{msg} locals, &block\n#{@_source}\nend"
|
419
|
+
|
420
|
+
if @_options[:source_location]
|
421
|
+
self.class.class_eval(src, @_options[:source_location])
|
422
|
+
else
|
423
|
+
self.class.class_eval(src)
|
424
|
+
end
|
420
425
|
end
|
426
|
+
|
427
|
+
send msg, locals, &block
|
421
428
|
end
|
422
429
|
end
|
423
430
|
end
|
data/lib/rabl/helpers.rb
CHANGED
@@ -14,7 +14,9 @@ module Rabl
|
|
14
14
|
# data_object_attribute(data) => @_object.send(data)
|
15
15
|
def data_object_attribute(data)
|
16
16
|
attribute = @_object.__send__(data)
|
17
|
-
attribute = attribute.as_json if
|
17
|
+
attribute = attribute.as_json if
|
18
|
+
is_collection?(attribute, false) &&
|
19
|
+
attribute.respond_to?(:as_json)
|
18
20
|
attribute
|
19
21
|
end
|
20
22
|
|
@@ -81,8 +83,10 @@ module Rabl
|
|
81
83
|
# is_collection?([]) => true
|
82
84
|
def is_collection?(obj, follow_symbols = true)
|
83
85
|
data_obj = follow_symbols ? data_object(obj) : obj
|
84
|
-
data_obj &&
|
85
|
-
|
86
|
+
data_obj &&
|
87
|
+
data_obj.is_a?(Enumerable) &&
|
88
|
+
!(data_obj.is_a?(Struct) ||
|
89
|
+
defined?(Hashie::Mash) && data_obj.is_a?(Hashie::Mash))
|
86
90
|
end
|
87
91
|
|
88
92
|
# Returns the context_scope wrapping this engine, used for retrieving data, invoking methods, etc
|
data/lib/rabl/version.rb
CHANGED
data/test/silence.rb
CHANGED
@@ -3,6 +3,15 @@ module Kernel
|
|
3
3
|
with_warnings(nil) { yield }
|
4
4
|
end
|
5
5
|
|
6
|
+
def with_warnings(flag)
|
7
|
+
old_verbose, $VERBOSE = $VERBOSE, flag
|
8
|
+
yield
|
9
|
+
ensure
|
10
|
+
$VERBOSE = old_verbose
|
11
|
+
end
|
12
|
+
end unless Kernel.respond_to? :silence_warnings
|
13
|
+
|
14
|
+
module Kernel
|
6
15
|
def silence_stream(stream)
|
7
16
|
old_stream = stream.dup
|
8
17
|
stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
|
@@ -11,11 +20,4 @@ module Kernel
|
|
11
20
|
ensure
|
12
21
|
stream.reopen(old_stream)
|
13
22
|
end
|
14
|
-
|
15
|
-
def with_warnings(flag)
|
16
|
-
old_verbose, $VERBOSE = $VERBOSE, flag
|
17
|
-
yield
|
18
|
-
ensure
|
19
|
-
$VERBOSE = old_verbose
|
20
|
-
end
|
21
|
-
end unless Kernel.respond_to? :silence_warnings
|
23
|
+
end unless Kernel.respond_to? :silence_stream
|
data/test/teststrap.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rabl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.14.
|
4
|
+
version: 0.14.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Esquenazi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -631,7 +631,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
631
631
|
version: '0'
|
632
632
|
requirements: []
|
633
633
|
rubyforge_project: rabl
|
634
|
-
rubygems_version: 2.
|
634
|
+
rubygems_version: 2.6.7
|
635
635
|
signing_key:
|
636
636
|
specification_version: 4
|
637
637
|
summary: General ruby templating with json, bson, xml and msgpack support
|