Class: Section

Inherits:
Hash
  • Object
show all
Defined in:
lib/madvertise/configuration.rb

Overview

A Configuration consists of one or more Sections. A section is a hash-like object that responds to all keys in the hash as if they were methods:

> s = Section.from_hash({:v1 => 2, :nested => {:v2 => 1}})
> s.v1
=> 2
> s.nested.v2
=> 1

Direct Known Subclasses

Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Hash

#deep_merge, #deep_merge!, #delta_merge!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/madvertise/configuration.rb', line 65

def method_missing(name, *args)
  if name.to_s =~ /(.*)=$/
    self[$1.to_sym] = Section.from_value(args.first)
  else
    value = self[name]
    self[name] = value.call if value.is_a?(Proc)
    self[name]
  end
end

Class Attribute Details

.nil_actionObject

How to handle nil values in the configuration?

Possible values are:

- :nil, nil  (return nil)
- :raise  (raise an exception)
- :section  (return a NilSection which can be chained)


31
32
33
# File 'lib/madvertise/configuration.rb', line 31

def nil_action
  @nil_action
end

Class Method Details

.from_hash(hsh) ⇒ Section

Create a new section from the given hash-like object.

Parameters:

  • hsh (Hash)

    The hash to convert into a section.

Returns:



37
38
39
40
41
42
43
# File 'lib/madvertise/configuration.rb', line 37

def from_hash(hsh)
  new.tap do |result|
    hsh.each do |key, value|
      result[key.to_sym] = from_value(value)
    end
  end
end