Module: Current
- Defined in:
- lib/rails_current.rb,
lib/rails_current.rb,
lib/rails_current.rb
Defined Under Namespace
Classes: BlankSlate, Proxy
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/rails_current.rb', line 148
def method_missing(method, *args, &block)
case method.to_s
when /^current_(.*)$/
msg = $1
value = Current.send(msg, *args, &block)
else
super
end
end
|
Class Method Details
.attribute(name, *args, &block) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/rails_current.rb', line 45
def Current.attribute(name, *args, &block)
options = Map.options_for!(args)
name = name.to_s
attribute_names.push(name)
attribute_names.uniq!
if options.has_key?(:default)
default = options[:default]
if default.respond_to?(:call)
block ||= default
else
data[name] = default
end
end
if !args.empty?
value = args.shift
data[name] = value
end
if block
generators[name] = block
end
define_attribute_method(name)
self
end
|
.attribute?(name) ⇒ Boolean
114
115
116
|
# File 'lib/rails_current.rb', line 114
def Current.attribute?(name)
attribute_names.include?(name.to_s)
end
|
.attribute_names ⇒ Object
118
119
120
|
# File 'lib/rails_current.rb', line 118
def Current.attribute_names
@attribute_names ||= []
end
|
.attributes ⇒ Object
122
123
124
|
# File 'lib/rails_current.rb', line 122
def Current.attributes
attribute_names.inject(Map.new){|map, name| map.update(name => send(name))}
end
|
.clear ⇒ Object
29
30
31
32
|
# File 'lib/rails_current.rb', line 29
def Current.clear
data.clear
self
end
|
.data ⇒ Object
25
26
27
|
# File 'lib/rails_current.rb', line 25
def Current.data
Thread.current[:current] ||= Map.new
end
|
.define_attribute_method(name) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/rails_current.rb', line 76
def Current.define_attribute_method(name)
name = name.to_s
unless respond_to?(name)
singleton_class.module_eval do
define_method(name) do
value = data[name]
if value.nil? and generator = generators[name]
value = generator.call
data[name] = value
end
value
end
define_method(name + '=') do |value|
data[name] = value
end
define_method(name + '?') do |*args|
send(name)
end
end
end
end
|
.dependencies ⇒ Object
8
9
10
11
12
|
# File 'lib/rails_current.rb', line 8
def Current.dependencies
{
'map' => [ 'map' , ' >= 6.0.1' ]
}
end
|
.ensure_rails_application(&block) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
# File 'lib/rails_current.rb', line 194
def Current.ensure_rails_application(&block)
require 'rails' unless defined?(Rails)
if Rails.application.nil?
mock = Class.new(Rails::Application)
Rails.application = mock.instance
begin
block.call()
ensure
Rails.application = nil
end
else
block.call()
end
end
|
.generators ⇒ Object
41
42
43
|
# File 'lib/rails_current.rb', line 41
def Current.generators
@generators ||= Map.new
end
|
.install_before_filter! ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/rails_current.rb', line 245
def Current.install_before_filter!
if defined?(::ActionController::Base)
::ActionController::Base.module_eval do
prepend_before_filter do |controller|
Current.clear
Current.controller = Current.proxy_for(controller)
Current.action = controller ? controller.send(:action_name) : nil
end
extend Current
include Current
helper{ include Current }
end
end
end
|
.method_missing(method, *args, &block) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/rails_current.rb', line 126
def Current.method_missing(method, *args, &block)
case method.to_s
when /^(.*)[=]$/
name = $1
value = args.shift
attribute(name, value)
value
when /^(.*)[?]$/
nil
else
if block
name = method.to_s
attribute(name, &block)
block
else
nil
end
end
end
|
.mock_controller(options = {}) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/rails_current.rb', line 159
def Current.mock_controller(options = {})
require 'rails'
require 'action_controller'
require 'action_dispatch/testing/test_request.rb'
require 'action_dispatch/testing/test_response.rb'
default_url_options =
begin
require 'rails_default_url_options'
DefaultUrlOptions
rescue LoadError
options[:default_url_options] || {}
end
ensure_rails_application do
store = ActiveSupport::Cache::MemoryStore.new
controller =
begin
ApplicationController.new
rescue NameError
ActionController::Base.new
end
controller.perform_caching = true
controller.cache_store = store
request = ActionDispatch::TestRequest.new
response = ActionDispatch::TestResponse.new
controller.request = request
controller.response = response
controller.send(:default_url_options).merge!(default_url_options)
Current.proxy_for(controller)
end
end
|
.proxy_for(object) ⇒ Object
223
224
225
|
# File 'lib/rails_current.rb', line 223
def Current.proxy_for(object)
Proxy.new(object)
end
|
.reset ⇒ Object
34
35
36
37
38
39
|
# File 'lib/rails_current.rb', line 34
def Current.reset
attribute_names.each{|name| undefine_attribute_method(name)}
attribute_names.clear
generators.clear
clear
end
|
.singleton_class ⇒ Object
110
111
112
|
# File 'lib/rails_current.rb', line 110
def Current.singleton_class
@singleton_class ||= class << self; self; end
end
|
.undefine_attribute_method(name) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/rails_current.rb', line 100
def Current.undefine_attribute_method(name)
if respond_to?(name)
singleton_class.module_eval do
remove_method(name)
remove_method(name + '=')
remove_method(name + '?')
end
end
end
|
.version ⇒ Object
4
5
6
|
# File 'lib/rails_current.rb', line 4
def Current.version
'1.7.0'
end
|