Class: Prefab::Context

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/prefab/context.rb', line 80

def initialize(context = {})
  @contexts = {}
  @seen_at = Time.now.utc.to_i

  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.



44
45
46
# File 'lib/prefab/context.rb', line 44

def contexts
  @contexts
end

#seen_atObject (readonly)

Returns the value of attribute seen_at.



44
45
46
# File 'lib/prefab/context.rb', line 44

def seen_at
  @seen_at
end

Class Method Details

.clear_currentObject



71
72
73
# File 'lib/prefab/context.rb', line 71

def clear_current
  Thread.current[THREAD_KEY] = nil
end

.currentObject



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

def current
  Thread.current[THREAD_KEY] ||= new
end

.current=(context) ⇒ Object



47
48
49
# File 'lib/prefab/context.rb', line 47

def current=(context)
  Thread.current[THREAD_KEY] = context
end

.merge_with_current(new_context_properties = {}) ⇒ Object



75
76
77
# File 'lib/prefab/context.rb', line 75

def merge_with_current(new_context_properties = {})
  new(current.to_h.merge(new_context_properties))
end

.with_context(context) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/prefab/context.rb', line 55

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

.with_merged_context(context) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/prefab/context.rb', line 63

def with_merged_context(context)
  old_context = Thread.current[THREAD_KEY]
  Thread.current[THREAD_KEY] = merge_with_current(context)
  yield
ensure
  Thread.current[THREAD_KEY] = old_context
end

Instance Method Details

#<=>(other) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/prefab/context.rb', line 171

def <=>(other)
  if other.is_a?(Prefab::Context)
    to_h <=> other.to_h
  else
    super
  end
end

#blank?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/prefab/context.rb', line 102

def blank?
  contexts.empty?
end

#clearObject



125
126
127
# File 'lib/prefab/context.rb', line 125

def clear
  @contexts = {}
end

#context(name) ⇒ Object



129
130
131
# File 'lib/prefab/context.rb', line 129

def context(name)
  contexts[name.to_s] || NamedContext.new(name, {})
end

#get(property_key) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/prefab/context.rb', line 110

def get(property_key)
  name, key = property_key.split('.', 2)

  if key.nil?
    name = BLANK_CONTEXT_NAME
    key = property_key
  end

  contexts[name]&.get(key)
end

#grouped_keyObject



164
165
166
167
168
# File 'lib/prefab/context.rb', line 164

def grouped_key
  contexts.map do |_, context|
    context.key
  end.sort.join('|')
end

#merge_default(defaults) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/prefab/context.rb', line 133

def merge_default(defaults)
  defaults.keys.each do |name|
    set(name, context(name).merge!(defaults[name]))
  end

  self
end

#set(name, hash) ⇒ Object



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

def set(name, hash)
  @contexts[name.to_s] = NamedContext.new(name, hash)
end

#slim_protoObject



156
157
158
159
160
161
162
# File 'lib/prefab/context.rb', line 156

def slim_proto
  PrefabProto::ContextSet.new(
    contexts: contexts.map do |_, context|
      context.to_proto
    end
  )
end

#to_hObject



121
122
123
# File 'lib/prefab/context.rb', line 121

def to_h
  contexts.transform_values(&:to_h)
end

#to_proto(namespace) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/prefab/context.rb', line 141

def to_proto(namespace)
  prefab_context = {
    'current-time' => ConfigValueWrapper.wrap(Prefab::TimeHelpers.now_in_ms)
  }

  prefab_context['namespace'] = ConfigValueWrapper.wrap(namespace) if namespace&.length&.positive?

  PrefabProto::ContextSet.new(
    contexts: contexts.map do |name, context|
      context.to_proto
    end.concat([PrefabProto::Context.new(type: 'prefab',
                                         values: prefab_context)])
  )
end