Class: Configurable::ConfigHash
- Inherits:
-
Object
- Object
- Configurable::ConfigHash
- Defined in:
- lib/configurable/config_hash.rb
Overview
ConfigHash acts like a hash that maps get and set operations as specified in by a receiver’s class configurations. Non-configuration keys are sent to an underlying data store.
Constant Summary collapse
- EMPTY_HASH =
A frozen empty hash returned by configs for unbound config hashes.
{}.freeze
Instance Attribute Summary collapse
-
#receiver ⇒ Object
readonly
The bound receiver.
-
#store ⇒ Object
readonly
The underlying data store; setting values in store directly can result in an inconsistent state.
Instance Method Summary collapse
-
#==(another) ⇒ Object
Equal if the to_hash values of self and another are equal.
-
#[](key) ⇒ Object
Retrieves the value for the key, either from the receiver or the store.
-
#[]=(key, value) ⇒ Object
Stores a value for the key, either on the receiver or in the store.
-
#bind(receiver) ⇒ Object
Binds the configs in store to the receiver by setting each on the receiver (via config.set).
-
#bound? ⇒ Boolean
Returns true if bound to a receiver.
-
#configs ⇒ Object
Returns the class configs for the bound receiver, or an empty hash if unbound (specifically EMPTY_HASH).
-
#consistent? ⇒ Boolean
Returns true if bound to a receiver and no configs values are set in store (ie all config values are stored on the receiver).
-
#each_pair ⇒ Object
Calls block once for each key-value pair stored in self.
-
#export ⇒ Object
Returns self exported as a hash of raw configs (ie name keys and uncast values).
-
#has_key?(key) ⇒ Boolean
True if the key is a key in configs or store.
-
#import(another) ⇒ Object
Imports a hash of raw configs (ie name keys and uncast values) and merges the result with self.
-
#initialize(store = {}, receiver = nil) ⇒ ConfigHash
constructor
Initializes a new ConfigHash.
-
#inspect ⇒ Object
Returns an inspection string.
-
#keys ⇒ Object
Returns the union of configs and store keys.
-
#merge!(another) ⇒ Object
Merges another with self.
- #parse(argv = ARGV, options = {}, &block) ⇒ Object
- #parse!(argv = ARGV, options = {}, &block) ⇒ Object
- #parser(options = {}, &block) ⇒ Object
-
#to_hash ⇒ Object
Returns self as a hash.
-
#unbind ⇒ Object
Unbinds the configs set on receiver by getting each value (via config.get) and setting the result into store.
Constructor Details
#initialize(store = {}, receiver = nil) ⇒ ConfigHash
Initializes a new ConfigHash.
18 19 20 21 |
# File 'lib/configurable/config_hash.rb', line 18 def initialize(store={}, receiver=nil) @store = store @receiver = receiver end |
Instance Attribute Details
#receiver ⇒ Object (readonly)
The bound receiver
11 12 13 |
# File 'lib/configurable/config_hash.rb', line 11 def receiver @receiver end |
#store ⇒ Object (readonly)
The underlying data store; setting values in store directly can result in an inconsistent state. Use []= instead.
15 16 17 |
# File 'lib/configurable/config_hash.rb', line 15 def store @store end |
Instance Method Details
#==(another) ⇒ Object
Equal if the to_hash values of self and another are equal.
125 126 127 |
# File 'lib/configurable/config_hash.rb', line 125 def ==(another) another.respond_to?(:to_hash) && to_hash == another.to_hash end |
#[](key) ⇒ Object
Retrieves the value for the key, either from the receiver or the store.
73 74 75 76 77 78 79 |
# File 'lib/configurable/config_hash.rb', line 73 def [](key) if config = configs[key] config.get(receiver) else store[key] end end |
#[]=(key, value) ⇒ Object
Stores a value for the key, either on the receiver or in the store.
82 83 84 85 86 87 88 |
# File 'lib/configurable/config_hash.rb', line 82 def []=(key, value) if config = configs[key] config.set(receiver, value) else store[key] = value end end |
#bind(receiver) ⇒ Object
Binds the configs in store to the receiver by setting each on the receiver (via config.set). Bound configs are removed from store.
Unbinds self from the current receiver, if needed.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/configurable/config_hash.rb', line 27 def bind(receiver) unbind if bound? @receiver = receiver configs.each_pair do |key, config| value = store.has_key?(key) ? store.delete(key) : config.default config.set(receiver, value) end self end |
#bound? ⇒ Boolean
Returns true if bound to a receiver.
53 54 55 |
# File 'lib/configurable/config_hash.rb', line 53 def bound? @receiver ? true : false end |
#configs ⇒ Object
Returns the class configs for the bound receiver, or an empty hash if unbound (specifically EMPTY_HASH).
59 60 61 62 63 64 |
# File 'lib/configurable/config_hash.rb', line 59 def configs # Caching here is not necessary or preferred as configurations are # cached on the class (which allows late inclusion of configurable # modules to work properly). bound? ? receiver.class.configs : EMPTY_HASH end |
#consistent? ⇒ Boolean
Returns true if bound to a receiver and no configs values are set in store (ie all config values are stored on the receiver).
68 69 70 |
# File 'lib/configurable/config_hash.rb', line 68 def consistent? bound? && (store.keys & configs.keys).empty? end |
#each_pair ⇒ Object
Calls block once for each key-value pair stored in self.
114 115 116 117 118 119 120 121 122 |
# File 'lib/configurable/config_hash.rb', line 114 def each_pair # :yields: key, value configs.each_pair do |key, config| yield(key, config.get(receiver)) end store.each_pair do |key, value| yield(key, value) end end |
#export ⇒ Object
Returns self exported as a hash of raw configs (ie name keys and uncast values).
145 146 147 |
# File 'lib/configurable/config_hash.rb', line 145 def export configs.export(to_hash) end |
#has_key?(key) ⇒ Boolean
True if the key is a key in configs or store.
96 97 98 |
# File 'lib/configurable/config_hash.rb', line 96 def has_key?(key) configs.has_key?(key) || store.has_key?(key) end |
#import(another) ⇒ Object
Imports a hash of raw configs (ie name keys and uncast values) and merges the result with self.
151 152 153 |
# File 'lib/configurable/config_hash.rb', line 151 def import(another) merge! configs.import(another) end |
#inspect ⇒ Object
Returns an inspection string.
169 170 171 |
# File 'lib/configurable/config_hash.rb', line 169 def inspect "#<#{self.class}:#{object_id} to_hash=#{to_hash.inspect}>" end |
#keys ⇒ Object
Returns the union of configs and store keys.
91 92 93 |
# File 'lib/configurable/config_hash.rb', line 91 def keys configs.keys | store.keys end |
#merge!(another) ⇒ Object
Merges another with self.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/configurable/config_hash.rb', line 101 def merge!(another) configs = self.configs another.each_pair do |key, value| if config = configs[key] config.set(receiver, value) else store[key] = value end end self end |
#parse(argv = ARGV, options = {}, &block) ⇒ Object
155 156 157 |
# File 'lib/configurable/config_hash.rb', line 155 def parse(argv=ARGV, ={}, &block) parse!(argv.dup, , &block) end |
#parse!(argv = ARGV, options = {}, &block) ⇒ Object
159 160 161 |
# File 'lib/configurable/config_hash.rb', line 159 def parse!(argv=ARGV, ={}, &block) parser(, &block).parse!(argv) end |
#parser(options = {}, &block) ⇒ Object
163 164 165 166 |
# File 'lib/configurable/config_hash.rb', line 163 def parser(={}, &block) = {:assign_defaults => false}.merge() configs.to_parser(self, , &block) end |
#to_hash ⇒ Object
Returns self as a hash. Any ConfigHash values are recursively hashified, to account for nesting.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/configurable/config_hash.rb', line 131 def to_hash hash = {} each_pair do |key, value| if value.kind_of?(ConfigHash) value = value.to_hash end hash[key] = value end hash end |
#unbind ⇒ Object
Unbinds the configs set on receiver by getting each value (via config.get) and setting the result into store. Does nothing if no receiver is set.
42 43 44 45 46 47 48 49 50 |
# File 'lib/configurable/config_hash.rb', line 42 def unbind if bound? configs.each_pair do |key, config| store[key] = config.get(receiver) end @receiver = nil end self end |