Module: Gon
- Defined in:
- lib/gon.rb,
lib/gon/rabl.rb,
lib/gon/helpers.rb,
lib/gon/version.rb,
lib/gon/jbuilder.rb
Defined Under Namespace
Modules: GonHelpers, Helpers, Jbuilder, Rabl
Constant Summary
collapse
- VERSION =
'2.1.0'
Class Method Summary
collapse
Class Method Details
.all_variables ⇒ Object
17
18
19
|
# File 'lib/gon.rb', line 17
def all_variables
@request_env[:gon]
end
|
.all_variables=(values) ⇒ Object
21
22
23
|
# File 'lib/gon.rb', line 21
def all_variables=(values)
raise "You can't use Gon public methods for storing data"
end
|
.clear ⇒ Object
25
26
27
|
# File 'lib/gon.rb', line 25
def clear
@request_env[:gon] = {}
end
|
.get_variable(name) ⇒ Object
59
60
61
|
# File 'lib/gon.rb', line 59
def get_variable(name)
@request_env[:gon][name]
end
|
.jbuilder(view_path, options = {}) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/gon.rb', line 89
def jbuilder(view_path, options = {})
if RUBY_VERSION !~ /9/
raise NoMethodError.new('You can use Jbuilder support only in 1.9+')
elsif !defined?(Gon::Jbuilder)
raise NoMethodError.new('You should define Jbuilder in your Gemfile')
end
jbuilder_data = Gon::Jbuilder.parse_jbuilder(view_path, options[:controller] ||
@request_env['action_controller.instance'] ||
@request_env['action_controller.rescue.response'].
instance_variable_get('@template').
instance_variable_get('@controller'))
if options[:as]
set_variable(options[:as].to_s, jbuilder_data)
elsif jbuilder_data.is_a? Hash
jbuilder_data.each do |key, value|
set_variable(key, value)
end
else
set_variable('jbuilder', jbuilder_data)
end
end
|
.method_missing(m, *args, &block) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/gon.rb', line 48
def method_missing(m, *args, &block)
if ( m.to_s =~ /=$/ )
if public_methods.include? m.to_s[0..-2].to_sym
raise "You can't use Gon public methods for storing data"
end
set_variable(m.to_s.delete('='), args[0])
else
get_variable(m.to_s)
end
end
|
.rabl(view_path, options = {}) ⇒ Object
TODO: Remove required argument view_path, and by default use current action
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/gon.rb', line 68
def rabl(view_path, options = {})
if !defined?(Gon::Rabl)
raise NoMethodError.new('You should define Rabl in your Gemfile')
end
rabl_data = Gon::Rabl.parse_rabl(view_path, options[:controller] ||
@request_env['action_controller.instance'] ||
@request_env['action_controller.rescue.response'].
instance_variable_get('@template').
instance_variable_get('@controller'))
if options[:as]
set_variable(options[:as].to_s, rabl_data)
elsif rabl_data.is_a? Hash
rabl_data.each do |key, value|
set_variable(key, value)
end
else
set_variable('rabl', rabl_data)
end
end
|
.request ⇒ Object
40
41
42
|
# File 'lib/gon.rb', line 40
def request
@request_id if defined? @request_id
end
|
.request=(request_id) ⇒ Object
44
45
46
|
# File 'lib/gon.rb', line 44
def request=(request_id)
@request_id = request_id
end
|
.request_env ⇒ Object
34
35
36
37
38
|
# File 'lib/gon.rb', line 34
def request_env
if defined?(@request_env)
return @request_env
end
end
|
.request_env=(environment) ⇒ Object
29
30
31
32
|
# File 'lib/gon.rb', line 29
def request_env=(environment)
@request_env = environment
@request_env[:gon] ||= {}
end
|
.set_variable(name, value) ⇒ Object
63
64
65
|
# File 'lib/gon.rb', line 63
def set_variable(name, value)
@request_env[:gon][name] = value
end
|