Permalink
Browse files

Thread options added

Used like this:

    Slim::Engine.with_options(...) do
       render ...
    end
  • Loading branch information...
1 parent 72cc1e2 commit 8a776a36db2d8207e0ebdf51eb1fc0ed74e28d50 @minad minad committed Sep 21, 2012
Showing with 25 additions and 3 deletions.
  1. +1 −0 CHANGES
  2. +1 −0 lib/temple/hash.rb
  3. +23 −3 lib/temple/mixins/options.rb
View
@@ -7,6 +7,7 @@ master
* HTML::AttributeRemover removed (Was too Slim specific)
* Engine option :chain removed
* Option validation implemented (Use define_options in your filters)
+ * ThreadOptions added, Method #with_options
0.4.1
View
@@ -95,6 +95,7 @@ def add_deprecated_keys(*keys)
end
def validate_hash!(hash)
+ raise ArgumentError, "Not a hash" unless hash.respond_to?(:keys)
hash.keys.each {|key| validate_key!(key) }
end
@@ -42,18 +42,38 @@ def disable_option_validator!
end
end
+ module ThreadOptions
+ def thread_options_key
+ @thread_options_key ||= "#{self.name}-thread-options".to_sym
+ end
+
+ def with_options(opts)
+ Thread.current[thread_options_key] = opts
+ yield
+ ensure
+ Thread.current[thread_options_key] = nil
+ end
+
+ def thread_options
+ Thread.current[thread_options_key]
+ end
+ end
+
# @api public
module Options
def self.included(base)
- base.class_eval { extend DefaultOptions }
+ base.class_eval do
+ extend DefaultOptions
+ extend ThreadOptions
+ end
end
attr_reader :options
def initialize(opts = {})
- raise ArgumentError, "Options must be given as hash" unless opts.keys
self.class.default_options.validate_hash!(opts)
- @options = ImmutableHash.new(opts, self.class.default_options)
+ self.class.default_options.validate_hash!(self.class.thread_options) if self.class.thread_options
+ @options = ImmutableHash.new(opts, self.class.thread_options, self.class.default_options)
end
end
end

0 comments on commit 8a776a3

Please sign in to comment.