Class: Bocuse::Configuration
- Inherits:
-
BlankSlate
- Object
- BlankSlate
- Bocuse::Configuration
- Defined in:
- lib/bocuse/configuration.rb
Overview
This is the core class of bocuse.
Its functions are:
* to be able to spit out a configuration hash when prompted.
* to be configurable
It will mainly return proxies that will lodge themselves in its internal hash. The proxies usually represent an internal hash value.
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#unresolved_block ⇒ Object
readonly
Returns the value of attribute unresolved_block.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Explicit accessor for POROs.
-
#[]=(key, value) ⇒ Object
Sets key to value.
-
#call(block) ⇒ Object
Executes the block such that it may modify this object.
-
#dup(&block) ⇒ Object
Performs a deep duplication of this configuration object.
-
#initialize(hash = nil, &block) ⇒ Configuration
constructor
A new instance of Configuration.
-
#method_missing(name, *args) ⇒ Object
The main method.
-
#to_h ⇒ Object
Returns a configuration hash.
Constructor Details
#initialize(hash = nil, &block) ⇒ Configuration
Returns a new instance of Configuration.
22 23 24 25 26 |
# File 'lib/bocuse/configuration.rb', line 22 def initialize(hash=nil, &block) @store = hash && hash.dup || Hash.new call(block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
The main method.
It will react to method calls, install the right keys on the internal store and return proxies on which callers can perform operations, e.g. <<.
Note: The user only interacts with Configurations. Not with the internal values.
Except when the user explicitly takes the values out using [].
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bocuse/configuration.rb', line 55 def method_missing name, *args case args.size when 0 if block_given? subconfig = Configuration.new &Proc.new store[name] = subconfig else store[name] ||= Value.new end when 1 store[name] = Value.new args.first end end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
19 20 21 |
# File 'lib/bocuse/configuration.rb', line 19 def store @store end |
#unresolved_block ⇒ Object (readonly)
Returns the value of attribute unresolved_block.
19 20 21 |
# File 'lib/bocuse/configuration.rb', line 19 def unresolved_block @unresolved_block end |
Instance Method Details
#[](key) ⇒ Object
Explicit accessor for POROs.
Note: Unwraps Values.
32 33 34 35 |
# File 'lib/bocuse/configuration.rb', line 32 def [] key value = @store[key] value.to_h rescue value end |
#[]=(key, value) ⇒ Object
Sets key to value.
39 40 41 |
# File 'lib/bocuse/configuration.rb', line 39 def []= key, value @store[key] = value end |
#call(block) ⇒ Object
Executes the block such that it may modify this object.
100 101 102 103 104 105 |
# File 'lib/bocuse/configuration.rb', line 100 def call(block) fail ArgumentError unless block block.call(self) if block.arity>0 instance_eval(&block) if block.arity == 0 end |
#dup(&block) ⇒ Object
Performs a deep duplication of this configuration object. This is mainly used by the user for quickly generating a lot of copies of a template.
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bocuse/configuration.rb', line 87 def dup(&block) dup_cfg = Configuration.new store.each do |key, value| dup_cfg[key] = value.dup end dup_cfg.call(block) if block dup_cfg end |
#to_h ⇒ Object
Returns a configuration hash.
NOTE: Resolves any unresolved blocks.
NOTE: Call to_json on this to get a JSON representation of the hash.
75 76 77 78 79 80 81 82 |
# File 'lib/bocuse/configuration.rb', line 75 def to_h copy = {} store.each do |key, value| value = value.to_h if value.respond_to?(:to_h) copy[key] = value if value end copy end |