Skip to content

Commit d13e829

Browse files
committed
Continue refactoring
1 parent 862f176 commit d13e829

File tree

7 files changed

+178
-100
lines changed

7 files changed

+178
-100
lines changed

lib/gon.rb

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717
module Gon
1818
class << self
1919

20-
def all_variables
21-
Request.gon
22-
end
23-
24-
def clear
25-
Request.clear
26-
end
27-
2820
def method_missing(method, *args, &block)
2921
if ( method.to_s =~ /=$/ )
3022
if public_method_name? method
@@ -37,6 +29,31 @@ def method_missing(method, *args, &block)
3729
end
3830
end
3931

32+
def all_variables
33+
Request.gon
34+
end
35+
36+
def clear
37+
Request.clear
38+
end
39+
40+
def rabl(*args)
41+
data, options = Gon::Rabl.handler(args)
42+
43+
store_builder_data 'rabl', data, options
44+
end
45+
46+
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+
52+
data, options = Gon::Jbuilder.handler(args)
53+
54+
store_builder_data 'jbuilder', data, options
55+
end
56+
4057
def inspect
4158
'Gon'
4259
end
@@ -51,6 +68,18 @@ def set_variable(name, value)
5168
Request.gon[name] = value
5269
end
5370

71+
def store_builder_data(builder, data, options)
72+
if options[:as]
73+
set_variable(options[:as].to_s, data)
74+
elsif data.is_a? Hash
75+
data.each do |key, value|
76+
set_variable(key, value)
77+
end
78+
else
79+
set_variable(builder, data)
80+
end
81+
end
82+
5483
def public_method_name?(method)
5584
public_methods.include?(
5685
if RUBY_VERSION > '1.9'

lib/gon/base.rb

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,24 @@ def global
66
Global
77
end
88

9-
protected
10-
11-
def check_method(method, *args, &block)
12-
13-
end
14-
15-
private
16-
17-
18-
19-
def get_variable(name)
20-
raise NotImplementedError
21-
end
22-
23-
def set_variable(name, value)
24-
raise NotImplementedError
25-
end
26-
27-
%w(rabl jbuilder).each do |builder_name|
28-
define_method builder_name do |*options|
29-
if options.first.is_a? String
30-
warn "[DEPRECATION] view_path argument is now optional. If you need to specify it please use #{builder}(:template => 'path')"
31-
options = options.extract_options!.merge(:template => options[0])
32-
else
33-
options = (options && options.first.is_a?(Hash)) ? options.first : { }
9+
def render_data(options)
10+
data = Gon.all_variables
11+
namespace = options[:namespace] || 'gon'
12+
start = '<script>window.' + namespace + ' = {};'
13+
script = ''
14+
15+
if options[:camel_case]
16+
data.each do |key, val|
17+
script << "#{namespace}.#{key.to_s.camelize(:lower)}=#{val.to_json};"
3418
end
35-
36-
builder_module = get_builder(builder_name)
37-
38-
data = builder_module.send("parse_#{builder_name}", get_template_path(options, builder_name), get_controller(options))
39-
40-
if options[:as]
41-
set_variable(options[:as].to_s, data)
42-
elsif data.is_a? Hash
43-
data.each do |key, value|
44-
set_variable(key, value)
45-
end
46-
else
47-
set_variable(builder_name, data)
19+
else
20+
data.each do |key, val|
21+
script << "#{namespace}.#{key.to_s}=#{val.to_json};"
4822
end
4923
end
50-
end
51-
alias_method :orig_jbuilder, :jbuilder
5224

53-
def jbuilder(*options)
54-
raise NoMethodError.new('You can use Jbuilder support only in 1.9+') if RUBY_VERSION < '1.9'
55-
orig_jbuilder(*options)
56-
end
57-
58-
private
59-
60-
def get_builder(builder_name)
61-
begin
62-
"Gon::#{builder_name.classify}".constantize
63-
rescue
64-
raise NoMethodError.new("You should define #{builder_name.classify} in your Gemfile")
65-
end
25+
script = start + Gon::Escaper.escape(script) + '</script>'
26+
script.html_safe
6627
end
6728

6829
def get_controller(options)
@@ -75,12 +36,24 @@ def get_controller(options)
7536

7637
def get_template_path(options, extension)
7738
if options[:template]
78-
File.extname(options[:template]) == ".#{extension}" ? options[:template] : "#{options[:template]}.#{extension}"
39+
if right_extension?(extension, options[:template])
40+
options[:template]
41+
else
42+
[options[:template], extension].join('.')
43+
end
7944
else
80-
"app/views/#{get_controller(options).controller_path}/#{get_controller(options).action_name}.json.#{extension}"
45+
controller = get_controller(options).controller_path
46+
action = get_controller(options).action_name
47+
"app/views/#{controller}/#{action}.json.#{extension}"
8148
end
8249
end
8350

51+
private
52+
53+
def right_extension?(extension, template_path)
54+
File.extname(template_path) == ".#{extension}"
55+
end
56+
8457
end
8558
end
8659
end

lib/gon/helpers.rb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@ def self.included base
88
module InstanceMethods
99

1010
def include_gon(options = {})
11-
if Gon.request_env && Gon.all_variables.present? && Gon.request == request.object_id
12-
data = Gon.all_variables
13-
namespace = options[:namespace] || 'gon'
14-
start = '<script>window.' + namespace + ' = {};'
15-
script = ''
16-
if options[:camel_case]
17-
data.each do |key, val|
18-
script << namespace + '.' + key.to_s.camelize(:lower) + '=' + val.to_json + ';'
19-
end
20-
else
21-
data.each do |key, val|
22-
script << namespace + '.' + key.to_s + '=' + val.to_json + ';'
23-
end
24-
end
25-
script = start + Gon::Escaper.escape(script) + '</script>'
26-
script.html_safe
11+
if variables_for_request_present?
12+
Gon::Base.render_data(options)
2713
else
2814
""
2915
end
3016
end
3117

18+
private
19+
20+
def variables_for_request_present?
21+
Gon::Request.env &&
22+
Gon::Request.id == request.object_id &&
23+
Gon.all_variables.present?
24+
end
25+
3226
end
3327
end
3428

@@ -41,13 +35,20 @@ def self.included base
4135
module InstanceMethods
4236

4337
def gon
44-
if !Gon.request_env || Gon.request != request.object_id
45-
Gon.request = request.object_id
46-
Gon.request_env = request.env
38+
if wrong_gon_request?
39+
Gon::Request.id = request.object_id
40+
Gon::Request.env = request.env
4741
end
4842
Gon
4943
end
5044

45+
private
46+
47+
def wrong_gon_request?
48+
Gon::Request.env.blank? ||
49+
Gon::Request.id != request.object_id
50+
end
51+
5152
end
5253

5354
end

lib/gon/jbuilder.rb

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,58 @@ module Gon
22
module Jbuilder
33
class << self
44

5-
def parse_source(source, controller)
6-
output = ::JbuilderTemplate.encode(controller) do |json|
7-
eval source
5+
def handler(args)
6+
options = parse_options_from args
7+
8+
data = parse_jbuilder \
9+
Gon::Base.get_template_path(options,'jbuilder'),
10+
Gon::Base.get_controller(options)
11+
12+
[data, options]
13+
end
14+
15+
private
16+
17+
def parse_options_from(args)
18+
if old_api? args
19+
text = "[DEPRECATION] view_path argument is now optional. "
20+
text << "If you need to specify it, "
21+
text << "please use gon.rabl(:template => 'path')"
22+
warn text
23+
24+
args.extract_options!.merge(:template => args[0])
25+
elsif new_api? args
26+
args.first
27+
else
28+
{}
829
end
9-
JSON.parse(output)
30+
end
31+
32+
def old_api?(args)
33+
args.first.is_a? String
34+
end
35+
36+
def new_api?(args)
37+
args.first.is_a? Hash
1038
end
1139

1240
def parse_jbuilder(jbuilder_path, controller)
1341
controller.instance_variables.each do |name|
14-
self.instance_variable_set(name, controller.instance_variable_get(name))
42+
self.instance_variable_set \
43+
name,
44+
controller.instance_variable_get(name)
1545
end
16-
lines = find_partials(File.readlines(jbuilder_path))
46+
lines = find_partials File.readlines(jbuilder_path)
1747
source = lines.join('')
1848

19-
output = parse_source(source, controller)
49+
output = parse_source source, controller
50+
end
51+
52+
def parse_source(source, controller)
53+
output = ::JbuilderTemplate.encode(controller) do |json|
54+
eval source
55+
end
56+
JSON.parse(output)
2057
end
2158

2259
def parse_partial(partial_line)
@@ -29,7 +66,7 @@ def parse_partial(partial_line)
2966
eval "def #{name}; self.instance_variable_get('@' + '#{name.to_s}'); end"
3067
end
3168
end
32-
find_partials(File.readlines(path))
69+
find_partials File.readlines(path)
3370
end
3471

3572
def find_partials(lines = [])

lib/gon/rabl.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,48 @@ module Gon
44
module Rabl
55
class << self
66

7+
def handler(args)
8+
options = parse_options_from args
9+
10+
data = parse_rabl \
11+
Gon::Base.get_template_path(options, 'rabl'),
12+
Gon::Base.get_controller(options)
13+
14+
[data, options]
15+
end
16+
17+
private
18+
719
def parse_rabl(rabl_path, controller)
820
source = File.read(rabl_path)
921
rabl_engine = ::Rabl::Engine.new(source, :format => 'json')
1022
output = rabl_engine.render(controller, {})
1123
JSON.parse(output)
1224
end
1325

26+
def parse_options_from(args)
27+
if old_api? args
28+
text = "[DEPRECATION] view_path argument is now optional. "
29+
text << "If you need to specify it, "
30+
text << "please use gon.rabl(:template => 'path')"
31+
warn text
32+
33+
args.extract_options!.merge(:template => args[0])
34+
elsif new_api? args
35+
args.first
36+
else
37+
{}
38+
end
39+
end
40+
41+
def old_api?(args)
42+
args.first.is_a? String
43+
end
44+
45+
def new_api?(args)
46+
args.first.is_a? Hash
47+
end
48+
1449
end
1550
end
1651
end

lib/gon/request.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ def env=(environment)
1111
@request_env['gon'] ||= {}
1212
end
1313

14-
def request
14+
def id
1515
@request_id if defined? @request_id
1616
end
1717

18-
def request=(request_id)
18+
def id=(request_id)
1919
@request_id = request_id
2020
end
2121

0 commit comments

Comments
 (0)