Class: OpenStudio::Workflow::Registry
- Inherits:
-
Object
- Object
- OpenStudio::Workflow::Registry
- Defined in:
- lib/openstudio/workflow/registry.rb
Instance Method Summary collapse
- #__internal_state ⇒ Object
-
#empty? ⇒ Boolean
Checks if this registry has any items.
-
#eval(key) ⇒ Object
Re-evaluate the proc of a key and update the cache.
-
#get(key) ⇒ Object
(also: #[])
Get the cached value of the given key.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Checks if the given key is registered with the registry.
-
#keys ⇒ Array
Returns an array populated with the keys of this object.
-
#length ⇒ Fixnum
(also: #size)
Return the number of elements in this registry.
-
#merge(other) ⇒ Registry
Merge one registry with another and return a completely new registry.
-
#merge!(other) ⇒ Void
Like #merge but updates self.
-
#register(key, &block) ⇒ Object
Register a key and cache it’s value.
-
#to_hash ⇒ Hash
Converts the registry to a hash.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
50 51 52 53 |
# File 'lib/openstudio/workflow/registry.rb', line 50 def initialize @items = {} @results_cache = {} end |
Instance Method Details
#__internal_state ⇒ Object
164 165 166 167 168 169 |
# File 'lib/openstudio/workflow/registry.rb', line 164 def __internal_state { items: @items, results_cache: @results_cache } end |
#empty? ⇒ Boolean
Checks if this registry has any items
124 125 126 |
# File 'lib/openstudio/workflow/registry.rb', line 124 def empty? @items.keys.empty? end |
#eval(key) ⇒ Object
Re-evaluate the proc of a key and update the cache
84 85 86 87 88 89 90 91 92 |
# File 'lib/openstudio/workflow/registry.rb', line 84 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
72 73 74 75 |
# File 'lib/openstudio/workflow/registry.rb', line 72 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
98 99 100 |
# File 'lib/openstudio/workflow/registry.rb', line 98 def key?(key) @items.key?(key) end |
#keys ⇒ Array
Returns an array populated with the keys of this object
107 108 109 |
# File 'lib/openstudio/workflow/registry.rb', line 107 def keys @items.keys end |
#length ⇒ Fixnum Also known as: size
Return the number of elements in this registry
115 116 117 |
# File 'lib/openstudio/workflow/registry.rb', line 115 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
134 135 136 137 138 139 |
# File 'lib/openstudio/workflow/registry.rb', line 134 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
146 147 148 149 |
# File 'lib/openstudio/workflow/registry.rb', line 146 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
61 62 63 64 65 |
# File 'lib/openstudio/workflow/registry.rb', line 61 def register(key, &block) raise ArgumentError, 'block required' unless block_given? @items[key] = block @results_cache[key] = @items[key].call end |
#to_hash ⇒ Hash
Converts the registry to a hash
155 156 157 158 159 160 161 162 |
# File 'lib/openstudio/workflow/registry.rb', line 155 def to_hash result = {} @results_cache.each_pair do |key, value| result[key] = value end result end |