Class: Flamingo::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/flamingo/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Config

Returns a new instance of Config.



8
9
10
# File 'lib/flamingo/config.rb', line 8

def initialize(hash={})
  @data = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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
# File 'lib/flamingo/config.rb', line 12

def method_missing(name,*args,&block)
  if name.to_s =~ /(.+)=$/
    @data[$1] = *args
  else
    value = @data[name.to_s]
    if value.is_a?(Hash)
      self.class.new(value)
    elsif value.nil? || empty_config?(value)
      if !args.empty?
        # Return a default if the value isn't set and there's an argument
        args.length == 1 ? args[0] : args
      elsif block_given?
        # Run the block to get the default value
        yield
      else
        # Return back a config object
        value = self.class.new
        @data[name.to_s] = value
        value
      end
    else
      value
    end
  end
end

Class Method Details

.load(file) ⇒ Object



4
5
6
# File 'lib/flamingo/config.rb', line 4

def self.load(file)
  new(YAML.load(IO.read(file)))
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/flamingo/config.rb', line 42

def empty?
  @data.empty?
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/flamingo/config.rb', line 38

def respond_to?(name)
  true
end