Class: BrainzLab::Context
- Inherits:
-
Object
- Object
- BrainzLab::Context
- Defined in:
- lib/brainzlab/context.rb
Constant Summary collapse
- THREAD_KEY =
:brainzlab_context
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#breadcrumbs ⇒ Object
readonly
Returns the value of attribute breadcrumbs.
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#request_headers ⇒ Object
Returns the value of attribute request_headers.
-
#request_id ⇒ Object
Returns the value of attribute request_id.
-
#request_method ⇒ Object
Returns the value of attribute request_method.
-
#request_params ⇒ Object
Returns the value of attribute request_params.
-
#request_path ⇒ Object
Returns the value of attribute request_path.
-
#request_url ⇒ Object
Returns the value of attribute request_url.
-
#session_id ⇒ Object
Returns the value of attribute session_id.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #data_hash ⇒ Object
-
#initialize ⇒ Context
constructor
A new instance of Context.
- #set_context(**data) ⇒ Object
- #set_tags(**data) ⇒ Object
- #set_user(id: nil, email: nil, name: nil, **extra) ⇒ Object
- #to_hash ⇒ Object
- #with_context(**data) ⇒ Object
Constructor Details
#initialize ⇒ Context
Returns a new instance of Context.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/brainzlab/context.rb', line 21 def initialize @user = {} @extra = {} @tags = {} @request_id = nil @session_id = nil @request_method = nil @request_path = nil @request_url = nil @request_params = nil @request_headers = nil @controller = nil @action = nil @stack = [] @breadcrumbs = Reflex::Breadcrumbs.new end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def action @action end |
#breadcrumbs ⇒ Object (readonly)
Returns the value of attribute breadcrumbs.
19 20 21 |
# File 'lib/brainzlab/context.rb', line 19 def @breadcrumbs end |
#controller ⇒ Object
Returns the value of attribute controller.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def controller @controller end |
#extra ⇒ Object (readonly)
Returns the value of attribute extra.
19 20 21 |
# File 'lib/brainzlab/context.rb', line 19 def extra @extra end |
#request_headers ⇒ Object
Returns the value of attribute request_headers.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_headers @request_headers end |
#request_id ⇒ Object
Returns the value of attribute request_id.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_id @request_id end |
#request_method ⇒ Object
Returns the value of attribute request_method.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_method @request_method end |
#request_params ⇒ Object
Returns the value of attribute request_params.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_params @request_params end |
#request_path ⇒ Object
Returns the value of attribute request_path.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_path @request_path end |
#request_url ⇒ Object
Returns the value of attribute request_url.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def request_url @request_url end |
#session_id ⇒ Object
Returns the value of attribute session_id.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def session_id @session_id end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
19 20 21 |
# File 'lib/brainzlab/context.rb', line 19 def @tags end |
#user ⇒ Object
Returns the value of attribute user.
17 18 19 |
# File 'lib/brainzlab/context.rb', line 17 def user @user end |
Class Method Details
.clear! ⇒ Object
12 13 14 |
# File 'lib/brainzlab/context.rb', line 12 def clear! Thread.current[THREAD_KEY] = nil end |
.current ⇒ Object
8 9 10 |
# File 'lib/brainzlab/context.rb', line 8 def current Thread.current[THREAD_KEY] ||= new end |
Instance Method Details
#data_hash ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/brainzlab/context.rb', line 72 def data_hash merged = @extra.dup @stack.each { |ctx| merged.merge!(ctx) } merged[:user] = @user unless @user.empty? merged[:tags] = @tags unless @tags.empty? merged end |
#set_context(**data) ⇒ Object
42 43 44 |
# File 'lib/brainzlab/context.rb', line 42 def set_context(**data) @extra.merge!(data) end |
#set_tags(**data) ⇒ Object
46 47 48 |
# File 'lib/brainzlab/context.rb', line 46 def (**data) @tags.merge!(data) end |
#set_user(id: nil, email: nil, name: nil, **extra) ⇒ Object
38 39 40 |
# File 'lib/brainzlab/context.rb', line 38 def set_user(id: nil, email: nil, name: nil, **extra) @user = { id: id, email: email, name: name }.compact.merge(extra) end |
#to_hash ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/brainzlab/context.rb', line 57 def to_hash result = {} result[:request_id] = @request_id if @request_id result[:session_id] = @session_id if @session_id merged_extra = @extra.dup @stack.each { |ctx| merged_extra.merge!(ctx) } result[:user] = @user unless @user.empty? result[:tags] = @tags unless @tags.empty? result[:context] = merged_extra unless merged_extra.empty? result end |
#with_context(**data) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/brainzlab/context.rb', line 50 def with_context(**data) push_context(data) yield ensure pop_context end |