Class: Prefab::Context

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#contextsObject (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_currentObject



50
51
52
# File 'lib/prefab/context.rb', line 50

def clear_current
  Thread.current[THREAD_KEY] = nil
end

.currentObject



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

#clearObject



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_hObject



107
108
109
# File 'lib/prefab/context.rb', line 107

def to_h
  contexts.map { |name, context| [name, context.to_h] }.to_h
end