rabl 0.9.2 → 0.9.3.pre1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmMxOGI3M2JkYjU2OWVjNjRlNTI2NjlkYTY0Y2Q4ZWI0ZjgxODI4MA==
4
+ NDI2NGZlZGFhNDhmN2U5M2VhMDI1YjIyNzJiNDllMjIxNjk0YzA2Zg==
5
5
  data.tar.gz: !binary |-
6
- MjE2YmIxNjQxMTllODk5NTcxNzgzNTUxOGUyZWFmNmIxM2VlOGYwZg==
6
+ NzE0NTBkZjVhMTJiNDZjOGNiYzhhODFiY2NjNGZiYmYyOWZiYWI0Nw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- OTU1YTdmZjc4MjBiMGYxMmJhODQxNTNlNDM4YmEzMTkzYjdmNzY1NTgzZWY1
10
- N2Y3Zjk4NTAyNTcyOTM5ZGQzZmFhZTcwM2VmNTIzYTEwM2Q1ZGYzNzViNTE2
11
- YTE1MjZhMTcxYjg0ZjYxZTlkYzA5ZWY3NGI3ODM0ZmQ1NjQxOTg=
9
+ NTZiMDRhYTdjOWQ3YTYyZTc5YWEyYWM4MTBiZjAxYzMxMmY2ZjZhZmM4NWMw
10
+ MGU3YWIxZjA3YjgxN2EyMTk5M2M3M2Y4ZTM1Mjc0ZWNhYzI3OGZkNzIxZTk2
11
+ OTg0ODNiYWYzZDg2NTRiZTMyZjJlYzUxMjVlZjI4YzU2MmNjNGE=
12
12
  data.tar.gz: !binary |-
13
- MGFmZmVhYThiOTY0NWM5OWViY2E3YTk5YjQ1YzRlZDZjNTEyNzUxNTIxZGFi
14
- MGE2Zjk5NzFmYzQ4M2E5MWYwZTFmYmVkNzJiNDNlNWNmZGQ1NTAxMTM1MjUx
15
- MzEzYjlmNzc2Y2MyMjg1MDM0ZTUzNzYyNmQ5ZWMzNmIxMTM2MTk=
13
+ OGQwMmY4NTJhMDQ5OTQ4Y2EyNmI4MzYyZmY2N2U3NGM1Y2MyNjg2NmZkMzcx
14
+ OGY5MGM0YTJjOThlMjMyNjk1ZWI3ZDhiZjc0NzE0NWM4NDlhZmY2MGM1ZGVj
15
+ MDlkZjA3ZjNhZDU3ZDUyOGJkNzFhYjYxMmIyNzc3ODU0Yzk1ZTQ=
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.9.3.pre1 (December 5th)
4
+
5
+ * FIX Send engine options through from render call (Thanks @bhicks)
6
+ * FIX Cleanup compile_hash method through refactoring complexity (Thanks @bhicks)
7
+ * FIX #503 handling render case with no lookup_context in Rails 4
8
+ * FIX #502 fix regression in render when object is nil with explicit root name
9
+
3
10
  ## 0.9.2 (November 21 2013)
4
11
 
5
12
  * FIX #508 by avoiding new proc syntax breaking 1.8.7
@@ -2,6 +2,13 @@ module Rabl
2
2
  class Builder
3
3
  include Rabl::Partials
4
4
 
5
+ SETTING_TYPES = {
6
+ :extends => :file,
7
+ :node => :name,
8
+ :child => :data,
9
+ :glue => :data
10
+ } unless const_defined? :SETTING_TYPES
11
+
5
12
  # Constructs a new rabl hash based on given object and options
6
13
  # options = { :format => "json", :root => true, :child_root => true,
7
14
  # :attributes, :node, :child, :glue, :extends }
@@ -29,44 +36,46 @@ module Rabl
29
36
  # compile_hash(:root_name => "user")
30
37
  def compile_hash(options={})
31
38
  @_result = {}
32
- # Extends
33
- @options[:extends].each do |settings|
34
- extends(settings[:file], settings[:options], &settings[:block])
35
- end if @options.has_key?(:extends)
36
- # Attributes
37
- @options[:attributes].each_pair do |attribute, settings|
38
- attribute(attribute, settings)
39
- end if @options.has_key?(:attributes)
40
- # Node
41
- @options[:node].each do |settings|
42
- node(settings[:name], settings[:options], &settings[:block])
43
- end if @options.has_key?(:node)
44
- # Children
45
- @options[:child].each do |settings|
46
- child(settings[:data], settings[:options], &settings[:block])
47
- end if @options.has_key?(:child)
48
- # Glues
49
- @options[:glue].each do |settings|
50
- glue(settings[:data], settings[:options], &settings[:block])
51
- end if @options.has_key?(:glue)
52
-
53
- # Wrap result in root
54
- if options[:root_name].present?
55
- @_root_name = options[:root_name]
39
+ update_settings(:extends)
40
+ update_attributes
41
+ update_settings(:node)
42
+ update_settings(:child)
43
+ update_settings(:glue)
44
+
45
+ wrap_result(options[:root_name])
46
+
47
+ replace_nil_values if Rabl.configuration.replace_nil_values_with_empty_strings
48
+
49
+ # Return Results
50
+ @_root_name ? { @_root_name => @_result } : @_result
51
+ end
52
+
53
+ def replace_nil_values
54
+ @_result = @_result.inject({}) do |hash, (k, v)|
55
+ hash[k] = v.nil? ? '' : v
56
+ hash
57
+ end
58
+ end
59
+
60
+ def wrap_result(root_name)
61
+ if root_name.present?
62
+ @_root_name = root_name
56
63
  else # no root
57
64
  @_root_name = nil
58
65
  end
66
+ end
59
67
 
60
- # Replace nil values with empty strings if configured
61
- if Rabl.configuration.replace_nil_values_with_empty_strings
62
- @_result = @_result.inject({}) do |hash, (k, v)|
63
- hash[k] = v.nil? ? '' : v
64
- hash
65
- end
66
- end
68
+ def update_settings(type)
69
+ settings_type = SETTING_TYPES[type]
70
+ @options[type].each do |settings|
71
+ send(type, settings[settings_type], settings[:options], &settings[:block])
72
+ end if @options.has_key?(type)
73
+ end
67
74
 
68
- # Return Results
69
- @_root_name ? { @_root_name => @_result } : @_result
75
+ def update_attributes
76
+ @options[:attributes].each_pair do |attribute, settings|
77
+ attribute(attribute, settings)
78
+ end if @options.has_key?(:attributes)
70
79
  end
71
80
 
72
81
  # Indicates an attribute or method should be included in the json output
@@ -36,7 +36,7 @@ module Rabl
36
36
  instance_eval(@_source) if @_source.present?
37
37
  end
38
38
  instance_exec(@_data_object, &block) if block_given?
39
- cache_results { self.send("to_" + @_options[:format].to_s) }
39
+ cache_results { self.send("to_" + @_options[:format].to_s, @_options) }
40
40
  end
41
41
 
42
42
  # Returns a hash representation of the data object
@@ -293,7 +293,10 @@ module Rabl
293
293
  @_options[:format],
294
294
  Digestor.digest(template, :rabl, lookup_context)
295
295
  ]
296
- end
296
+ rescue NameError => e # Handle case where lookup_context doesn't exist
297
+ raise e unless e.message =~ /lookup_context/
298
+ cache_key_simple(cache_key)
299
+ end # cache_key_with_digest
297
300
 
298
301
  def cache_key_simple(key)
299
302
  Array(key) + [@_options[:root_name], @_options[:format]]
@@ -49,7 +49,7 @@ module Rabl
49
49
  def determine_object_root(data_token, data_name=nil, include_root=true)
50
50
  return if object_root_name == false
51
51
  root_name = data_name.to_s if include_root
52
- if is_object?(data_token)
52
+ if is_object?(data_token) || data_token.nil?
53
53
  root_name
54
54
  elsif is_collection?(data_token)
55
55
  object_root_name || (root_name.singularize if root_name)
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.9.2"
2
+ VERSION = "0.9.3.pre1"
3
3
  end
@@ -99,6 +99,15 @@ context "Rabl::Engine" do
99
99
  template.render(scope)
100
100
  end.equals "{\"person\":{}}"
101
101
 
102
+ asserts "that it can set root node with a nil object and explicit name" do
103
+ template = rabl %q{
104
+ object @user => :person
105
+ }
106
+ scope = Object.new
107
+ scope.instance_variable_set :@user, nil
108
+ template.render(scope)
109
+ end.equals "{\"person\":{}}"
110
+
102
111
  asserts "that it can use non-ORM objects" do
103
112
  template = rabl %q{
104
113
  object @other
@@ -433,6 +442,16 @@ context "Rabl::Engine" do
433
442
  scope.instance_variable_set :@user, User.new
434
443
  template.render(scope)
435
444
  end.equals "{}"
445
+
446
+ asserts "that it can set root node with a nil object and explicit name" do
447
+ template = rabl %q{
448
+ object @user => :person
449
+ attributes :name
450
+ }
451
+ scope = Object.new
452
+ scope.instance_variable_set :@user, nil
453
+ template.render(scope)
454
+ end.equals "{}"
436
455
  end
437
456
 
438
457
  context "#collection" do
@@ -12,6 +12,28 @@ context "Rabl::Helpers" do
12
12
  @user = User.new
13
13
  end
14
14
 
15
+ # determine_object_root(@user, :user, true) => "user"
16
+ # determine_object_root(@user, :person) => "person"
17
+ # determine_object_root([@user, @user]) => "user"
18
+ # def determine_object_root(data_token, data_name=nil, include_root=true)
19
+ context "for determine_object_root method" do
20
+ asserts "returns nil if include_root is false" do
21
+ @helper_class.determine_object_root(@user, :user, false)
22
+ end.equals(nil)
23
+
24
+ asserts "returns user root name if include_root is true" do
25
+ @helper_class.determine_object_root(@user, :user, true)
26
+ end.equals("user")
27
+
28
+ asserts "returns explicit alias if specified" do
29
+ @helper_class.determine_object_root(@user, :person)
30
+ end.equals("person")
31
+
32
+ asserts "returns explicit alias if object is nil" do
33
+ @helper_class.determine_object_root(nil, :person)
34
+ end.equals("person")
35
+ end
36
+
15
37
  context "for data_name method" do
16
38
  asserts "returns nil if no data" do
17
39
  @helper_class.data_name(nil)
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.9.2
4
+ version: 0.9.3.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-22 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -461,9 +461,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
461
461
  version: '0'
462
462
  required_rubygems_version: !ruby/object:Gem::Requirement
463
463
  requirements:
464
- - - ! '>='
464
+ - - ! '>'
465
465
  - !ruby/object:Gem::Version
466
- version: '0'
466
+ version: 1.3.1
467
467
  requirements: []
468
468
  rubyforge_project: rabl
469
469
  rubygems_version: 2.0.7