Module: Contexts

Defined in:
lib/contexts.rb,
lib/contexts/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'2.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/contexts.rb', line 9

def self.included(base)
  base.class_eval do
    extend ClassMethods

    before_action :apply_contexts

    helper_method :contexts,
                  :current_context,
                  :locked_context,
                  :context_locked?
  end
end

.resolve(name) ⇒ Object



5
6
7
# File 'lib/contexts.rb', line 5

def self.resolve(name)
  "#{name}_context".classify.constantize.new
end

Instance Method Details

#context_locked?(name = nil) ⇒ Boolean



51
52
53
# File 'lib/contexts.rb', line 51

def context_locked?(name = nil)
  locked_context(name).present?
end

#contextsObject



33
34
35
# File 'lib/contexts.rb', line 33

def contexts
  @contexts ||= (request.env['contexts'] || {})
end

#current_context(name = nil) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/contexts.rb', line 37

def current_context(name = nil)
  if name
    (ctx = contexts[name]) && ctx.current
  else
    Hash[contexts.map{ |name, ctx| [ name, ctx.current ] }]
  end
end

#lock_context(data) ⇒ Object



55
56
57
58
59
# File 'lib/contexts.rb', line 55

def lock_context(data)
  ctx = locked_context
  ctx.merge!(data.stringify_keys).reject!{ |k, v| v.blank? }
  ctx
end

#locked_context(name = nil) ⇒ Object



45
46
47
48
49
# File 'lib/contexts.rb', line 45

def locked_context(name = nil)
  locked = (session[:locked_context] ||= {})

  name ? locked[name.to_s] : locked
end