Class: Castor::Configuration

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/castor/configuration.rb,
lib/castor/configuration/node.rb

Defined Under Namespace

Classes: InvalidValueError, Node

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ Configuration

Returns a new instance of Configuration.



5
6
7
8
9
# File 'lib/castor/configuration.rb', line 5

def initialize(block)
  @values = {}
  instance_eval(&block)
  @initialized = true
end

Instance Method Details

#def(name, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/castor/configuration.rb', line 11

def def(name, *args, &block)
  return super(name, *args, block) if @initialized

  options = args.last.is_a?(::Hash) ? args.pop : {}
  config_value = nil

  if options[:nested]
    config_value = Castor::Configuration.new(block)

    selfclass.define_method(name) do 
      config_value
    end
  else
    block = Proc.new {
      default (args.first || options.delete(:lazy))
    } unless block

    config_value = Castor::Configuration::Node.new(name, block)

    selfclass.send(:define_method, name) do
      config_value.value
    end

    selfclass.send(:define_method, "#{name}=") do |args|
      config_value.value = args
    end

    selfclass.send(:define_method, "#{name}!") do
      config_value
    end
  end
  
  @values[name] = config_value
end

#def_many(attributes) ⇒ Object



46
47
48
49
50
# File 'lib/castor/configuration.rb', line 46

def def_many(attributes)
  attributes.each do |key, value|
    self.def key, value
  end
end

#each(&block) ⇒ Object



56
57
58
# File 'lib/castor/configuration.rb', line 56

def each(&block)
  @values.each(&block)
end

#merge(hash) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/castor/configuration.rb', line 60

def merge(hash)
  hash.each do |k, v|
    if self.send(k).is_a?(Castor::Configuration)
      self.send(k).merge(v)
    else
      self.send("#{k}=", v)
    end
  end
end

#selfclassObject



52
53
54
# File 'lib/castor/configuration.rb', line 52

def selfclass
  class << self; self; end;
end