Class: OpenStudio::Workflow::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/workflow/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry



15
16
17
18
# File 'lib/openstudio/workflow/registry.rb', line 15

def initialize
  @items = {}
  @results_cache = {}
end

Instance Method Details

#__internal_stateObject



129
130
131
132
133
134
# File 'lib/openstudio/workflow/registry.rb', line 129

def __internal_state
  {
    items: @items,
    results_cache: @results_cache
  }
end

#empty?Boolean

Checks if this registry has any items



89
90
91
# File 'lib/openstudio/workflow/registry.rb', line 89

def empty?
  @items.keys.empty?
end

#eval(key) ⇒ Object

Re-evaluate the proc of a key and update the cache



49
50
51
52
53
54
55
56
57
# File 'lib/openstudio/workflow/registry.rb', line 49

def eval(key)
  return nil unless @items.key?(key)
  begin
    @items[key].call
  rescue
    return nil
  end
  @results_cache[key] = @items[key].call
end

#get(key) ⇒ Object Also known as: []

Get the cached value of the given key



37
38
39
40
# File 'lib/openstudio/workflow/registry.rb', line 37

def get(key)
  return nil unless @items.key?(key)
  @results_cache[key]
end

#key?(key) ⇒ Boolean Also known as: has_key?

Checks if the given key is registered with the registry



63
64
65
# File 'lib/openstudio/workflow/registry.rb', line 63

def key?(key)
  @items.key?(key)
end

#keysArray

Returns an array populated with the keys of this object



72
73
74
# File 'lib/openstudio/workflow/registry.rb', line 72

def keys
  @items.keys
end

#lengthFixnum Also known as: size

Return the number of elements in this registry



80
81
82
# File 'lib/openstudio/workflow/registry.rb', line 80

def length
  @items.keys.length
end

#merge(other) ⇒ Registry

Merge one registry with another and return a completely new registry. Note that the result cache is completely

busted, so any gets on the new registry will result in a cache miss


99
100
101
102
103
104
# File 'lib/openstudio/workflow/registry.rb', line 99

def merge(other)
  self.class.new.tap do |result|
    result.merge!(self)
    result.merge!(other)
  end
end

#merge!(other) ⇒ Void

Like #merge but updates self



111
112
113
114
# File 'lib/openstudio/workflow/registry.rb', line 111

def merge!(other)
  @items.merge!(other.__internal_state[:items])
  self
end

#register(key, &block) ⇒ Object

Register a key and cache it’s value. Note that if a key with the given name already exists it is overwritten

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/openstudio/workflow/registry.rb', line 26

def register(key, &block)
  raise ArgumentError, 'block required' unless block_given?
  @items[key] = block
  @results_cache[key] = @items[key].call
end

#to_hashHash

Converts the registry to a hash



120
121
122
123
124
125
126
127
# File 'lib/openstudio/workflow/registry.rb', line 120

def to_hash
  result = {}
  @results_cache.each_pair do |key, value|
    result[key] = value
  end

  result
end