Class: Hadley::Config

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(defaults = {}) ⇒ Config

Initializes this Config with the specified defaults.

Parameters:

  • defaults (Hash) (defaults to: {})

    The default configuration values for this Config instance.



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?

Delegates to #set, #get or #proc depending on the nature of the given name, if a block is given or if the args array is not empty.

Parameters:

  • name (String)

    The name of the property to be read or written. If the name ends with ‘=’ it will be stripped from the name and the operation will be treated as a write.

  • args (*Object)

    The optional array of property values to be assigned to the provided property name. If this array is not empty then #set will be called.

  • &block (Proc)

    The optional block to be assigned to the provided property name. If the operation has a block given then #proc will be called.

Returns:

  • (Object, nil)

    The value ultimately written to or read from the given property name.



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.

Parameters:

  • name (String)

    The name of the property to be read.

Returns:

  • (Object, nil)

    The value stored under the provided name or nil if no such value exists.



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.

Parameters:

  • name (String)

    The name of the property to be written.

  • &block (Proc)

    The block to be assigned to the provided property name.

Returns:

  • (Proc)

    The block written to the provided 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.

Parameters:

  • name (String)

    The name of the property to be written.

  • args (*Object)

    The value or values to be assigned to the provided property name. If a single value is found a scalar will be written otherwise an array will be written.

Returns:

  • (Object, nil)

    The value written to the provided 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