Skip to content

Commit

Permalink
ThreadOptions: support nested with_options
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Sep 21, 2012
1 parent 1f0f285 commit e272788
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/temple/mixins/options.rb
Expand Up @@ -28,7 +28,7 @@ def define_options(*opts)
default_options.add_valid_keys(opts) default_options.add_valid_keys(opts)
end end


def deprecated_options(*opts) def define_deprecated_options(*opts)
if opts.last.respond_to?(:keys) if opts.last.respond_to?(:keys)
hash = opts.pop hash = opts.pop
default_options.add_deprecated_keys(hash.keys) default_options.add_deprecated_keys(hash.keys)
Expand All @@ -43,20 +43,23 @@ def disable_option_validator!
end end


module ThreadOptions module ThreadOptions
def thread_options_key def with_options(options)
@thread_options_key ||= "#{self.name}-thread-options".to_sym old_options = thread_options
end Thread.current[thread_options_key] = ImmutableHash.new(options, thread_options)

def with_options(opts)
Thread.current[thread_options_key] = opts
yield yield
ensure ensure
Thread.current[thread_options_key] = nil Thread.current[thread_options_key] = old_options
end end


def thread_options def thread_options
Thread.current[thread_options_key] Thread.current[thread_options_key]
end end

protected

def thread_options_key
@thread_options_key ||= "#{self.name}-thread-options".to_sym
end
end end


# @api public # @api public
Expand Down

0 comments on commit e272788

Please sign in to comment.