Class: Prefab::Context
- Inherits:
-
Object
- Object
- Prefab::Context
- Defined in:
- lib/prefab/context.rb
Defined Under Namespace
Classes: NamedContext
Constant Summary collapse
- BLANK_CONTEXT_NAME =
''- THREAD_KEY =
:prefab_context
Instance Attribute Summary collapse
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Class Method Summary collapse
- .clear_current ⇒ Object
- .current ⇒ Object
- .current=(context) ⇒ Object
- .merge_with_current(new_context_properties = {}) ⇒ Object
- .with_context(context) ⇒ Object
Instance Method Summary collapse
- #[](property_key) ⇒ Object
- #[]=(name, hash) ⇒ Object
- #clear ⇒ Object
- #context(name) ⇒ Object
- #get(property_key) ⇒ Object
-
#initialize(context = {}) ⇒ Context
constructor
A new instance of Context.
- #merge!(name, hash) ⇒ Object
- #set(name, hash) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(context = {}) ⇒ Context
Returns a new instance of Context.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/prefab/context.rb', line 59 def initialize(context = {}) @contexts = {} if context.is_a?(NamedContext) @contexts[context.name] = context elsif context.is_a?(Hash) context.map do |name, values| if values.is_a?(Hash) @contexts[name.to_s] = NamedContext.new(name, values) else warn '[DEPRECATION] Prefab contexts should be a hash with a key of the context name and a value of a hash.' @contexts[BLANK_CONTEXT_NAME] ||= NamedContext.new(BLANK_CONTEXT_NAME, {}) @contexts[BLANK_CONTEXT_NAME].merge!({ name => values }) end end else raise ArgumentError, 'must be a Hash or a NamedContext' end end |
Instance Attribute Details
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
31 32 33 |
# File 'lib/prefab/context.rb', line 31 def contexts @contexts end |
Class Method Details
.clear_current ⇒ Object
50 51 52 |
# File 'lib/prefab/context.rb', line 50 def clear_current Thread.current[THREAD_KEY] = nil end |
.current ⇒ Object
38 39 40 |
# File 'lib/prefab/context.rb', line 38 def current Thread.current[THREAD_KEY] ||= new end |
.current=(context) ⇒ Object
34 35 36 |
# File 'lib/prefab/context.rb', line 34 def current=(context) Thread.current[THREAD_KEY] = context end |
.merge_with_current(new_context_properties = {}) ⇒ Object
54 55 56 |
# File 'lib/prefab/context.rb', line 54 def merge_with_current(new_context_properties = {}) new(current.to_h.merge(new_context_properties)) end |
.with_context(context) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/prefab/context.rb', line 42 def with_context(context) old_context = Thread.current[THREAD_KEY] Thread.current[THREAD_KEY] = new(context) yield ensure Thread.current[THREAD_KEY] = old_context end |
Instance Method Details
#[](property_key) ⇒ Object
103 104 105 |
# File 'lib/prefab/context.rb', line 103 def [](property_key) get(property_key) end |
#[]=(name, hash) ⇒ Object
88 89 90 |
# File 'lib/prefab/context.rb', line 88 def []=(name, hash) set(name, hash) end |
#clear ⇒ Object
111 112 113 |
# File 'lib/prefab/context.rb', line 111 def clear @contexts = {} end |
#context(name) ⇒ Object
115 116 117 |
# File 'lib/prefab/context.rb', line 115 def context(name) contexts[name.to_s] || NamedContext.new(name, {}) end |
#get(property_key) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/prefab/context.rb', line 92 def get(property_key) name, key = property_key.split('.', 2) if key.nil? name = BLANK_CONTEXT_NAME key = property_key end contexts[name] && contexts[name].get(key) end |
#merge!(name, hash) ⇒ Object
80 81 82 |
# File 'lib/prefab/context.rb', line 80 def merge!(name, hash) @contexts[name.to_s] = context(name).merge!(hash) end |
#set(name, hash) ⇒ Object
84 85 86 |
# File 'lib/prefab/context.rb', line 84 def set(name, hash) @contexts[name.to_s] = NamedContext.new(name, hash) end |
#to_h ⇒ Object
107 108 109 |
# File 'lib/prefab/context.rb', line 107 def to_h contexts.map { |name, context| [name, context.to_h] }.to_h end |