Class: Hadley::Config
- Inherits:
-
Object
- Object
- Hadley::Config
- Defined in:
- lib/hadley/config.rb
Overview
This class is a convenience wrapper around an Hash that provides a more expressive api for initial configuration and referencing the configuration information at runtime. For example:
config.prop 'value' # --> config[:prop] = 'value'
config.prop = 'value' # --> same as above
config.props 'a', 'b', 'c' # --> config[:props] = [ 'a', 'b', 'c' ]
config.props = 'a', 'b', 'c' # same as above
config.callback { |it| puts it } # config[:callback] = { |it| puts it }
config.prop # --> config[:prop]
Instance Method Summary collapse
-
#get(name) ⇒ Object?
Retrieves the value stored under the provided name.
-
#initialize(defaults = {}) ⇒ Config
constructor
Initializes this Config with the specified defaults.
- #method_missing(name, *args, &block) ⇒ Object?
-
#proc(name, &block) ⇒ Proc
Stores the given block under the provided property name.
-
#set(name, *args) ⇒ Object?
Stores the value or values indicated by the args array under the provided property name.
Constructor Details
#initialize(defaults = {}) ⇒ Config
Initializes this Config with the specified defaults.
14 15 16 |
# File 'lib/hadley/config.rb', line 14 def initialize(defaults={}) @config = defaults end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object?
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hadley/config.rb', line 29 def method_missing(name, *args, &block) if block_given? proc(name, &block) elsif name =~ /(.+)=$/ set($1, *args) elsif not args.empty? set(name, *args) else get(name) end end |
Instance Method Details
#get(name) ⇒ Object?
Retrieves the value stored under the provided name.
67 68 69 |
# File 'lib/hadley/config.rb', line 67 def get(name) @config[name.to_sym] end |
#proc(name, &block) ⇒ Proc
Stores the given block under the provided property name.
47 48 49 |
# File 'lib/hadley/config.rb', line 47 def proc(name, &block) @config[name.to_sym] = block end |
#set(name, *args) ⇒ Object?
Stores the value or values indicated by the args array under the provided property name.
58 59 60 |
# File 'lib/hadley/config.rb', line 58 def set(name, *args) @config[name.to_sym] = args.size == 1 ? args.first : args end |