Skip to content

Commit 2037861

Browse files
committed
Added Global module for working with global variables
1 parent df9b004 commit 2037861

File tree

6 files changed

+117
-63
lines changed

6 files changed

+117
-63
lines changed

lib/gon.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'action_view'
55
require 'action_controller'
66
require 'gon/base'
7+
require 'gon/global'
78
require 'gon/request'
89
require 'gon/helpers'
910
require 'gon/escaper'
@@ -17,6 +18,10 @@
1718
module Gon
1819
class << self
1920

21+
def global
22+
Gon::Global
23+
end
24+
2025
def method_missing(method, *args, &block)
2126
if ( method.to_s =~ /=$/ )
2227
if public_method_name? method
@@ -44,11 +49,6 @@ def rabl(*args)
4449
end
4550

4651
def jbuilder(*args)
47-
if RUBY_VERSION < '1.9'
48-
text = 'You can use Jbuilder support only in 1.9+'
49-
raise NoMethodError.new text
50-
end
51-
5252
data, options = Gon::Jbuilder.handler(args)
5353

5454
store_builder_data 'jbuilder', data, options

lib/gon/base.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ module Gon
22
module Base
33
class << self
44

5-
def global
6-
Global
7-
end
8-
95
def render_data(options)
10-
data = Gon.all_variables
6+
data = Gon.all_variables.merge(Gon.global.all_variables)
117
namespace = options[:namespace] || 'gon'
128
start = '<script>window.' + namespace + ' = {};'
139
script = ''

lib/gon/global.rb

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,63 @@ module Gon
22
module Global
33
class << self
44

5-
attr_reader :vars
5+
def all_variables
6+
@global_vars || {}
7+
end
8+
9+
def method_missing(method, *args, &block)
10+
@global_vars ||= {}
11+
if ( method.to_s =~ /=$/ )
12+
if public_method_name? method
13+
raise "You can't use Gon public methods for storing data"
14+
end
15+
16+
@global_vars[method.to_s.delete('=')] = args[0]
17+
else
18+
@global_vars[method.to_s]
19+
end
20+
end
21+
22+
def rabl(*args)
23+
data, options = Gon::Rabl.handler(args, true)
24+
25+
store_builder_data 'rabl', data, options
26+
end
27+
28+
def jbuilder(*args)
29+
data, options = Gon::Jbuilder.handler(args, true)
30+
31+
store_builder_data 'jbuilder', data, options
32+
end
33+
34+
def inspect
35+
'Gon'
36+
end
37+
38+
private
39+
40+
def store_builder_data(builder, data, options)
41+
if options[:as]
42+
@global_vars[options[:as].to_s] = data
43+
elsif data.is_a? Hash
44+
data.each do |key, value|
45+
@global_vars[key] = value
46+
end
47+
else
48+
@global_vars[builder] = data
49+
end
50+
end
51+
52+
def public_method_name?(method)
53+
public_methods.include?(
54+
if RUBY_VERSION > '1.9'
55+
method.to_s[0..-2].to_sym
56+
else
57+
method.to_s[0..-2]
58+
end
59+
)
60+
end
661

7-
def method_missing
8-
end
962
end
1063
end
1164
end

lib/gon/jbuilder.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ module Gon
22
module Jbuilder
33
class << self
44

5-
def handler(args)
5+
def handler(args, global = false)
66
options = parse_options_from args
7+
if global && !options[:template]
8+
raise 'You should provide :template when use rabl with global variables'
9+
end
710

811
data = parse_jbuilder \
912
Gon::Base.get_template_path(options,'jbuilder'),

lib/gon/rabl.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ module Gon
44
module Rabl
55
class << self
66

7-
def handler(args)
7+
def handler(args, global = false)
88
options = parse_options_from args
9+
if global && !options[:template]
10+
raise 'You should provide :template when use rabl with global variables'
11+
end
912

1013
data = parse_rabl \
1114
Gon::Base.get_template_path(options, 'rabl'),

spec/gon/gon_spec.rb

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
Gon.clear
7474
lambda { Gon.all_variables = 123 }.should raise_error
7575
lambda { Gon.rabl = 123 }.should raise_error
76-
lambda { Gon.request = 123 }.should raise_error
7776
end
7877

7978
describe '.rabl' do
@@ -143,75 +142,75 @@
143142

144143
end
145144

146-
require 'jbuilder'
147-
require 'gon/jbuilder'
145+
require 'jbuilder'
146+
require 'gon/jbuilder'
148147

149-
describe '.jbuilder' do
150-
context 'render jbuilder templates' do
148+
describe '.jbuilder' do
149+
context 'render jbuilder templates' do
151150

152-
before do
153-
Gon.clear
154-
controller.instance_variable_set('@objects', objects)
155-
end
156-
157-
let(:controller) { ActionController::Base.new }
158-
let(:objects) { [1,2] }
151+
before do
152+
Gon.clear
153+
controller.instance_variable_set('@objects', objects)
154+
end
159155

160-
it 'render json from jbuilder template' do
161-
Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller
162-
Gon.objects.length.should == 2
163-
end
156+
let(:controller) { ActionController::Base.new }
157+
let(:objects) { [1,2] }
164158

165-
it 'render json from jbuilder template with a partial' do
166-
controller.view_paths << 'spec/test_data'
167-
Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
168-
Gon.objects.length.should == 2
169-
end
159+
it 'render json from jbuilder template' do
160+
Gon.jbuilder 'spec/test_data/sample.json.jbuilder', :controller => controller
161+
Gon.objects.length.should == 2
162+
end
170163

164+
it 'render json from jbuilder template with a partial' do
165+
controller.view_paths << 'spec/test_data'
166+
Gon.jbuilder 'spec/test_data/sample_with_partial.json.jbuilder', :controller => controller
167+
Gon.objects.length.should == 2
171168
end
172169

173-
it 'should raise error if you use gon.jbuilder without requiring jbuilder gem' do
174-
Gon.send(:remove_const, :Jbuilder)
170+
end
175171

176-
expect { Gon.jbuilder 'some_path' }.to raise_error(NameError)
177-
load 'jbuilder.rb'
178-
load 'gon/jbuilder.rb'
179-
end
172+
it 'should raise error if you use gon.jbuilder without requiring jbuilder gem' do
173+
Gon.send(:remove_const, :Jbuilder)
180174

175+
expect { Gon.jbuilder 'some_path' }.to raise_error(NameError)
176+
load 'jbuilder.rb'
177+
load 'gon/jbuilder.rb'
181178
end
182179

183-
describe '.get_template_path' do
184-
context 'template is specified' do
180+
end
185181

186-
it 'add the extension if not included in the template name' do
187-
Gon::Base.send(:get_template_path, { :template => 'spec/test_data/sample'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder')
188-
end
182+
describe '.get_template_path' do
183+
context 'template is specified' do
189184

190-
it 'return the specified template' do
191-
Gon::Base.send(:get_template_path, { :template => 'spec/test_data/sample.jbuilder'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder')
192-
end
185+
it 'add the extension if not included in the template name' do
186+
Gon::Base.send(:get_template_path, { :template => 'spec/test_data/sample'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder')
187+
end
193188

189+
it 'return the specified template' do
190+
Gon::Base.send(:get_template_path, { :template => 'spec/test_data/sample.jbuilder'}, 'jbuilder').should eql('spec/test_data/sample.jbuilder')
194191
end
195192

196-
context 'template is not specified' do
193+
end
197194

198-
before do
199-
Gon.clear
200-
controller.instance_variable_set('@objects', objects)
201-
controller.action_name = 'show'
202-
end
195+
context 'template is not specified' do
203196

204-
let(:controller) { ActionController::Base.new }
205-
let(:objects) { [1,2] }
197+
before do
198+
Gon.clear
199+
controller.instance_variable_set('@objects', objects)
200+
controller.action_name = 'show'
201+
end
206202

207-
context 'the action doesn as a template at a different format' do
208-
it 'return the same template as the action with rabl extension' do
209-
Gon::Base.send(:get_template_path, {:controller => controller}, 'jbuilder').should eql('app/views/action_controller/base/show.json.jbuilder')
210-
end
211-
end
203+
let(:controller) { ActionController::Base.new }
204+
let(:objects) { [1,2] }
212205

206+
context 'the action doesn as a template at a different format' do
207+
it 'return the same template as the action with rabl extension' do
208+
Gon::Base.send(:get_template_path, {:controller => controller}, 'jbuilder').should eql('app/views/action_controller/base/show.json.jbuilder')
209+
end
213210
end
211+
214212
end
213+
end
215214

216215

217216
def request

0 commit comments

Comments
 (0)