Class: Configarrr::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/configarrr/base.rb

Direct Known Subclasses

Simple, YAML

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



3
4
5
6
# File 'lib/configarrr/base.rb', line 3

def initialize(options = {})
  @keys = []
  parse_options(options)
end

Class Method Details

.configure(options = {}, &block) ⇒ Object



8
9
10
11
12
# File 'lib/configarrr/base.rb', line 8

def self.configure(options = {}, &block)
  config = new(options)
  config.configure(&block)
  config
end

Instance Method Details

#configure(&block) ⇒ Object



14
15
16
# File 'lib/configarrr/base.rb', line 14

def configure(&block)
  instance_eval(&block)
end

#set(key, value = self) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/configarrr/base.rb', line 18

def set(key, value=self)
  if value.kind_of?(Proc)
    metadef(key, &value)
    metadef("#{key}?") { !!__send__(key) }
    metadef("#{key}=") { |val| set(key, Proc.new{val}) }
    @keys << key 
  elsif value == self && key.is_a?(Hash)
    key.to_hash.each { |k,v| set(k, v) }
  elsif respond_to?("#{key}=")
    __send__ "#{key}=", value
    @keys << key 
  else
    set key, Proc.new{value}
  end
  self
end

#to_hashObject



35
36
37
38
39
40
# File 'lib/configarrr/base.rb', line 35

def to_hash
  @keys.inject({}) do |hash, key|
    hash[key] = __send__ "#{key}"
    hash
  end
end