rabl 0.6.11 → 0.6.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,12 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.6.12
4
+
5
+ * Fix view_path options for renderer (Thanks @ivanvanderbyl and @route)
6
+ * Only escape if data exists
7
+ * Fix default object recognition for Rails 2.3.2
8
+ * Adds `root_object` method on engine (Thanks @OliverLetterer)
9
+
3
10
  ## 0.6.11
4
11
 
5
12
  * Changes send to __send__ (Thanks @alindeman)
@@ -9,6 +9,7 @@ module Rabl
9
9
  def initialize(options={}, &block)
10
10
  @options = options
11
11
  @_scope = options[:scope]
12
+ @_view_path = options[:view_path]
12
13
  end
13
14
 
14
15
  # Given an object and options, returns the hash representation
@@ -10,6 +10,7 @@ module Rabl
10
10
  def initialize(source, options={})
11
11
  @_source = source
12
12
  @_options = options
13
+ @_view_path = options[:view_path]
13
14
  end
14
15
 
15
16
  def source=(string)
@@ -109,6 +110,13 @@ module Rabl
109
110
  @_data = data unless @_locals[:object]
110
111
  end
111
112
 
113
+ # Returns the current object that is the topic of this template
114
+ # Can be the collection or the object depending on topic assigned
115
+ # root_object => @user
116
+ def root_object
117
+ @_data
118
+ end
119
+
112
120
  # Sets the object as a collection casted to a simple array
113
121
  # collection @users
114
122
  # collection @users => :people
@@ -186,8 +194,9 @@ module Rabl
186
194
  # default_object => @user
187
195
  def default_object
188
196
  if context_scope.respond_to?(:controller)
189
- full_name = context_scope.controller.controller_name
190
- instance_variable_get("@#{ full_name.split("::").last }")
197
+ controller_name = context_scope.controller.controller_name
198
+ stripped_name = controller_name.split(%r{::|\/}).last
199
+ instance_variable_get("@#{stripped_name}")
191
200
  end
192
201
  end
193
202
 
@@ -97,7 +97,7 @@ module Rabl
97
97
 
98
98
  # Escape output if configured and supported
99
99
  def escape_output(data)
100
- defined?(ERB::Util.h) && Rabl.configuration.escape_all_output ? ERB::Util.h(data) : data
100
+ (data && defined?(ERB::Util.h) && Rabl.configuration.escape_all_output) ? ERB::Util.h(data) : data
101
101
  end
102
102
 
103
103
  end
@@ -8,7 +8,7 @@ module Rabl
8
8
  # options can have :view_path, :child_root, :root
9
9
  def partial(file, options={}, &block)
10
10
  raise ArgumentError, "Must provide an :object option to render a partial" unless options.has_key?(:object)
11
- object, view_path = options.delete(:object), options.delete(:view_path)
11
+ object, view_path = options.delete(:object), options[:view_path] || @_view_path
12
12
  source, location = self.fetch_source(file, :view_path => view_path)
13
13
  engine_options = options.merge(:source => source, :source_location => location)
14
14
  self.object_to_hash(object, engine_options, &block)
@@ -23,7 +23,7 @@ module Rabl
23
23
  def object_to_hash(object, options={}, &block)
24
24
  return object unless is_object?(object) || is_collection?(object)
25
25
  return [] if is_collection?(object) && object.blank? # empty collection
26
- engine_options = options.reverse_merge(:format => "hash", :root => (options[:root] || false))
26
+ engine_options = options.reverse_merge(:format => "hash", :view_path => @_view_path, :root => (options[:root] || false))
27
27
  Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
28
28
  end
29
29
 
@@ -1,3 +1,3 @@
1
1
  module Rabl
2
- VERSION = "0.6.11"
2
+ VERSION = "0.6.12"
3
3
  end
@@ -8,11 +8,12 @@ context "Rabl::Builder" do
8
8
  setup do
9
9
  @users = [User.new, User.new]
10
10
  @user = User.new
11
- builder({})
11
+ builder({:view_path => '/path/to/views'})
12
12
  end
13
13
 
14
14
  context "#initialize" do
15
15
  asserts_topic.assigns :options
16
+ asserts_topic.assigns :_view_path
16
17
  end
17
18
 
18
19
  context "#build" do
@@ -14,6 +14,7 @@ context "Rabl::Engine" do
14
14
 
15
15
  asserts_topic.assigns :_source
16
16
  asserts_topic.assigns :_options
17
+ asserts_topic.assigns :_view_path
17
18
  end
18
19
 
19
20
  context "#cache" do
@@ -293,6 +294,16 @@ context "Rabl::Engine" do
293
294
  end
294
295
 
295
296
  context "#object" do
297
+ asserts "that it sets default object" do
298
+ template = rabl %{
299
+ attribute :name
300
+ }
301
+ scope = Object.new
302
+ stub(scope).controller { stub(Object).controller_name { "a/b/c::d/user" } }
303
+ scope.instance_variable_set :@user, User.new
304
+ template.render(scope).split
305
+ end.equals "{\"name\":\"rabl\"}".split
306
+
296
307
  asserts "that it sets data source" do
297
308
  template = rabl %q{
298
309
  object @user
@@ -371,6 +382,17 @@ context "Rabl::Engine" do
371
382
  template.render(scope)
372
383
  end.equals "{\"city\":\"irvine\"}"
373
384
 
385
+ asserts "that it exposes root_object" do
386
+ template = rabl %q{
387
+ object @user
388
+
389
+ attribute :name, :as => root_object.city
390
+ }
391
+ scope = Object.new
392
+ scope.instance_variable_set :@user, User.new(:name => 'irvine')
393
+ template.render(scope)
394
+ end.equals "{\"irvine\":\"irvine\"}"
395
+
374
396
  asserts "that it can add attribute under a different key name through hash" do
375
397
  template = rabl %{
376
398
  object @user
@@ -158,6 +158,35 @@ context "Rabl::Renderer" do
158
158
  scope.instance_variable_set :@users, nil
159
159
  Rabl.render([], 'test', :view_path => tmp_path, :root => false, :scope => scope)
160
160
  end.equals "[]"
161
+
162
+ asserts 'handles view path for when it specified and config is empty' do
163
+ Rabl.configuration.view_paths = []
164
+
165
+ File.open(tmp_path + "profile.json.rabl", "w") do |f|
166
+ f.puts %q{
167
+ attributes :gender
168
+ }
169
+ end
170
+
171
+ File.open(tmp_path + "user.json.rabl", "w") do |f|
172
+ f.puts %(
173
+ object @user
174
+ attribute :name
175
+ glue(:profile) do
176
+ extends 'profile'
177
+ end
178
+ child(:profile) do
179
+ extends 'profile'
180
+ end
181
+ )
182
+ end
183
+
184
+ user = User.new(:name => 'irvine')
185
+ stub(user).profile { stub!.gender { "male" } }
186
+
187
+ renderer = Rabl::Renderer.new('user', user, :view_path => tmp_path)
188
+ renderer.render.split("").sort
189
+ end.equals "{\"user\":{\"name\":\"irvine\",\"object\":{\"gender\":\"male\"},\"gender\":\"male\"}}".split("").sort
161
190
  end
162
191
 
163
192
  context '.json' do
metadata CHANGED
@@ -1,137 +1,183 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rabl
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.12
4
5
  prerelease:
5
- version: 0.6.11
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Nathan Esquenazi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-05-10 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: activesupport
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
23
21
  version: 2.3.14
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: multi_json
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.14
30
+ - !ruby/object:Gem::Dependency
31
+ name: multi_json
32
+ requirement: !ruby/object:Gem::Requirement
30
33
  none: false
31
- requirements:
34
+ requirements:
32
35
  - - ~>
33
- - !ruby/object:Gem::Version
34
- version: "1.0"
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
35
38
  type: :runtime
36
- version_requirements: *id002
37
- - !ruby/object:Gem::Dependency
38
- name: riot
39
39
  prerelease: false
40
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
- requirements:
42
+ requirements:
43
43
  - - ~>
44
- - !ruby/object:Gem::Version
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: riot
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
45
53
  version: 0.12.3
46
54
  type: :development
47
- version_requirements: *id003
48
- - !ruby/object:Gem::Dependency
49
- name: rr
50
55
  prerelease: false
51
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
52
57
  none: false
53
- requirements:
58
+ requirements:
54
59
  - - ~>
55
- - !ruby/object:Gem::Version
60
+ - !ruby/object:Gem::Version
61
+ version: 0.12.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: rr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
56
69
  version: 1.0.2
57
70
  type: :development
58
- version_requirements: *id004
59
- - !ruby/object:Gem::Dependency
60
- name: rake
61
71
  prerelease: false
62
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
63
73
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: "0"
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.2
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
68
86
  type: :development
69
- version_requirements: *id005
70
- - !ruby/object:Gem::Dependency
71
- name: tilt
72
87
  prerelease: false
73
- requirement: &id006 !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
74
89
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: tilt
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
79
102
  type: :development
80
- version_requirements: *id006
81
- - !ruby/object:Gem::Dependency
82
- name: yajl-ruby
83
103
  prerelease: false
84
- requirement: &id007 !ruby/object:Gem::Requirement
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: yajl-ruby
112
+ requirement: !ruby/object:Gem::Requirement
85
113
  none: false
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: "0"
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
90
118
  type: :development
91
- version_requirements: *id007
92
- - !ruby/object:Gem::Dependency
93
- name: msgpack
94
119
  prerelease: false
95
- requirement: &id008 !ruby/object:Gem::Requirement
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: msgpack
128
+ requirement: !ruby/object:Gem::Requirement
96
129
  none: false
97
- requirements:
130
+ requirements:
98
131
  - - ~>
99
- - !ruby/object:Gem::Version
132
+ - !ruby/object:Gem::Version
100
133
  version: 0.4.5
101
134
  type: :development
102
- version_requirements: *id008
103
- - !ruby/object:Gem::Dependency
104
- name: bson
105
135
  prerelease: false
106
- requirement: &id009 !ruby/object:Gem::Requirement
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 0.4.5
142
+ - !ruby/object:Gem::Dependency
143
+ name: bson
144
+ requirement: !ruby/object:Gem::Requirement
107
145
  none: false
108
- requirements:
146
+ requirements:
109
147
  - - ~>
110
- - !ruby/object:Gem::Version
148
+ - !ruby/object:Gem::Version
111
149
  version: 1.5.2
112
150
  type: :development
113
- version_requirements: *id009
114
- - !ruby/object:Gem::Dependency
115
- name: plist
116
151
  prerelease: false
117
- requirement: &id010 !ruby/object:Gem::Requirement
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.5.2
158
+ - !ruby/object:Gem::Dependency
159
+ name: plist
160
+ requirement: !ruby/object:Gem::Requirement
118
161
  none: false
119
- requirements:
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- version: "0"
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
123
166
  type: :development
124
- version_requirements: *id010
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
125
174
  description: General ruby templating with json, bson, xml and msgpack support
126
- email:
175
+ email:
127
176
  - nesquena@gmail.com
128
177
  executables: []
129
-
130
178
  extensions: []
131
-
132
179
  extra_rdoc_files: []
133
-
134
- files:
180
+ files:
135
181
  - .gitignore
136
182
  - .travis.yml
137
183
  - CHANGELOG.md
@@ -372,32 +418,29 @@ files:
372
418
  - test/teststrap.rb
373
419
  homepage: https://github.com/nesquena/rabl
374
420
  licenses: []
375
-
376
421
  post_install_message:
377
422
  rdoc_options: []
378
-
379
- require_paths:
423
+ require_paths:
380
424
  - lib
381
- required_ruby_version: !ruby/object:Gem::Requirement
425
+ required_ruby_version: !ruby/object:Gem::Requirement
382
426
  none: false
383
- requirements:
384
- - - ">="
385
- - !ruby/object:Gem::Version
386
- version: "0"
387
- required_rubygems_version: !ruby/object:Gem::Requirement
427
+ requirements:
428
+ - - ! '>='
429
+ - !ruby/object:Gem::Version
430
+ version: '0'
431
+ required_rubygems_version: !ruby/object:Gem::Requirement
388
432
  none: false
389
- requirements:
390
- - - ">="
391
- - !ruby/object:Gem::Version
392
- version: "0"
433
+ requirements:
434
+ - - ! '>='
435
+ - !ruby/object:Gem::Version
436
+ version: '0'
393
437
  requirements: []
394
-
395
438
  rubyforge_project: rabl
396
- rubygems_version: 1.8.19
439
+ rubygems_version: 1.8.21
397
440
  signing_key:
398
441
  specification_version: 3
399
442
  summary: General ruby templating with json, bson, xml and msgpack support
400
- test_files:
443
+ test_files:
401
444
  - test/bson_engine_test.rb
402
445
  - test/builder_test.rb
403
446
  - test/configuration_test.rb