lib/rails_current.rb in rails_current-1.3.0 vs lib/rails_current.rb in rails_current-1.4.0
- old
+ new
@@ -2,11 +2,11 @@
require 'map'
module Current
def Current.version
- '1.3.0'
+ '1.4.0'
end
def Current.data
Thread.current[:current] ||= Map.new
end
@@ -15,41 +15,62 @@
data.clear
self
end
def Current.reset
- calls.keys.each{|name, block| undefine_attribute_method(name) }
- data.clear
- calls.clear
- self
+ attribute_names.each{|name| undefine_attribute_method(name)}
+ attribute_names.clear
+ generators.clear
+ clear
end
+ def Current.generators
+ @generators ||= Map.new
+ end
+
def Current.attribute(name, *args, &block)
- options = Map.options_for(args)
+ options = Map.options_for!(args)
name = name.to_s
- default = options.has_key?(:default) ? options[:default] : args.shift
- block ||= proc{ default }
- calls[name] = block
+ 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
- def Current.calls
- @calls ||= Map.new
- end
-
def Current.define_attribute_method(name)
unless respond_to?(name)
singleton_class.module_eval do
define_method(name) do
if data.has_key?(name)
data[name]
else
- data[name] = calls[name].call
+ if generator = generators[name]
+ value = generator.call
+ data[name] = value
+ end
end
end
define_method(name + '=') do |value|
data[name] = value
@@ -75,15 +96,15 @@
def Current.singleton_class
@singleton_class ||= class << self; self; end
end
def Current.attribute?(name)
- calls.has_key?(name)
+ attribute_names.include?(name.to_s)
end
def Current.attribute_names
- calls.keys
+ @attribute_names ||= []
end
def Current.attributes
attribute_names.inject(Map.new){|map, name| map.update(name => send(name))}
end
@@ -91,11 +112,11 @@
def Current.method_missing(method, *args, &block)
case method.to_s
when /^(.*)[=]$/
name = $1
value = args.shift
- attribute(name){ value }
+ attribute(name, value)
value
when /^(.*)[?]$/
nil
@@ -126,24 +147,37 @@
end
if defined?(Rails)
+##
+#
module Current
attribute(:controller)
attribute(:user)
+ end
+##
+#
+ module Current
def Current.install_before_filter!
- ::ActionController::Base.module_eval do
- prepend_before_filter do |controller|
- Current.clear
- Current.controller = controller
+ if defined?(::ActionController::Base)
+ ::ActionController::Base.module_eval do
+ prepend_before_filter do |controller|
+ Current.clear
+ Current.controller = controller
+ end
+
+ include Current
+ helper{ include Current }
end
- end if defined?(::ActionController::Base)
+ end
end
end
+##
+#
if defined?(Rails::Engine)
class Engine < Rails::Engine
config.before_initialize do
ActiveSupport.on_load(:action_controller) do
Current.install_before_filter!
@@ -153,10 +187,9 @@
else
Current.install_before_filter!
end
end
-
::Rails_current = ::Current
BEGIN {
Object.send(:remove_const, :Current) if defined?(::Current)