Class: Cased::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/cased/context.rb,
lib/cased/context/expander.rb

Defined Under Namespace

Classes: Expander

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}) ⇒ Context

Returns a new instance of Context.



22
23
24
# File 'lib/cased/context.rb', line 22

def initialize(context = {})
  @context = Cased::Context::Expander.expand(context || {})
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



20
21
22
# File 'lib/cased/context.rb', line 20

def context
  @context
end

Class Method Details

.clear!Object



16
17
18
# File 'lib/cased/context.rb', line 16

def self.clear!
  Thread.current[:cased_context] = nil
end

.currentObject



8
9
10
# File 'lib/cased/context.rb', line 8

def self.current
  Thread.current[:cased_context] ||= new
end

.current=(context) ⇒ Object



12
13
14
# File 'lib/cased/context.rb', line 12

def self.current=(context)
  Thread.current[:cased_context] = new(context)
end

Instance Method Details

#[](key) ⇒ Object



42
43
44
# File 'lib/cased/context.rb', line 42

def [](key)
  @context[key]
end

#[]=(key, value) ⇒ Object



46
47
48
# File 'lib/cased/context.rb', line 46

def []=(key, value)
  merge(key => value)
end

#clearObject



26
27
28
# File 'lib/cased/context.rb', line 26

def clear
  @context = {}
end

#merge(new_context = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cased/context.rb', line 30

def merge(new_context = {})
  if block_given?
    old_context = @context.dup
    @context.deep_merge!(Cased::Context::Expander.expand(new_context))
    yield
  else
    @context.deep_merge!(Cased::Context::Expander.expand(new_context))
  end
ensure
  @context = old_context if block_given?
end