Class: OpenStudio::Workflow::Registry

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

Overview

TODO:

(rhorsey) registry should be a member of WorkflowRunner - DLM

TODO:

(rhorsey) how is this different than a regular hash? why is it important to be able to register keys with blocks that return values instead of values, looks like the block is called on insert anyway? let’s not go crazy on performance optimizations until we have to - DLM

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



22
23
24
25
# File 'lib/openstudio/workflow/registry.rb', line 22

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

Instance Method Details

#__internal_stateObject



139
140
141
142
143
144
# File 'lib/openstudio/workflow/registry.rb', line 139

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

#empty?Boolean

Checks if this registry has any items

Returns:

  • (Boolean)


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

def empty?
  @items.keys.empty?
end

#eval(key) ⇒ Object

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

Parameters:

  • key (Sym or String)

    This will evaluate the item assigned to the key and update the cache if possible

Returns:

  • If successful the method returns the new value, and if it cannot find or cannot update the key it

    returns nil



58
59
60
61
62
63
64
65
66
67
# File 'lib/openstudio/workflow/registry.rb', line 58

def eval(key)
  return nil unless @items.key?(key)

  begin
    @items[key].call
  rescue StandardError
    return nil
  end
  @results_cache[key] = @items[key].call
end

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

Get the cached value of the given key

Parameters:

  • []

    key The key defining the block

Returns:

  • Returns the registries cached value for the key or nil if the key was not found



45
46
47
48
49
# File 'lib/openstudio/workflow/registry.rb', line 45

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

Returns:

  • (Boolean)


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

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

#keysArray

Returns an array populated with the keys of this object

Returns:

  • (Array)


82
83
84
# File 'lib/openstudio/workflow/registry.rb', line 82

def keys
  @items.keys
end

#lengthInteger Also known as: size

Return the number of elements in this registry

Returns:

  • (Integer)


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

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

Parameters:

  • other (Registry)

    The other #Registry to merge onto of self

Returns:



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

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

Parameters:

  • other (Registry)

    The other #Registry to merge onto of self

Returns:

  • (Void)


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

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

Parameters:

  • []

    key The key for the passed in block. Symbols are highly recommended

  • block (Proc)

    The block (Proc) which contains the registered information

Returns:

  • Returns block.call from the registries cache

Raises:

  • (ArgumentError)


33
34
35
36
37
38
# File 'lib/openstudio/workflow/registry.rb', line 33

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

Returns:

  • (Hash)

    The registry as a hash



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

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

  result
end