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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prefab/context.rb', line 72

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



63
64
65
# File 'lib/prefab/context.rb', line 63

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



67
68
69
# File 'lib/prefab/context.rb', line 67

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

#blank?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/prefab/context.rb', line 94

def blank?
  contexts.empty?
end

#clearObject



117
118
119
# File 'lib/prefab/context.rb', line 117

def clear
  @contexts = {}
end

#context(name) ⇒ Object



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

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

#get(property_key) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/prefab/context.rb', line 102

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



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

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

#merge_default(defaults) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/prefab/context.rb', line 125

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

  self
end

#set(name, hash) ⇒ Object



98
99
100
# File 'lib/prefab/context.rb', line 98

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

#slim_protoObject



148
149
150
151
152
153
154
# File 'lib/prefab/context.rb', line 148

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

#to_hObject



113
114
115
# File 'lib/prefab/context.rb', line 113

def to_h
  contexts.transform_values(&:to_h)
end

#to_proto(namespace) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/prefab/context.rb', line 133

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