Class: Ficus

Inherits:
RecursiveOpenStruct show all
Defined in:
lib/ficus.rb

Defined Under Namespace

Classes: ConfigError

Constant Summary

Constants inherited from RecursiveOpenStruct

RecursiveOpenStruct::VERSION

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RecursiveOpenStruct

#debug_inspect, #display_recursive_open_struct, #initialize, #new_ostruct_member, #recurse_over_array, #to_h

Constructor Details

This class inherits a constructor from RecursiveOpenStruct

Class Attribute Details

.log(log = {}) ⇒ Object

Returns the value of attribute log.



7
8
9
# File 'lib/ficus.rb', line 7

def log
  @log
end

.verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/ficus.rb', line 7

def verbose
  @verbose
end

Class Method Details

.load(file, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ficus.rb', line 9

def load(file, &block)
  @log = []
  yaml = YAML.load File.read(file)
  config = Ficus.new(yaml, :recurse_over_arrays => true)
  config.instance_eval(&block) if block_given?

  errors = log.select{|v| v =~ /^\[ERR\]/}
  if errors.size > 0
    log.each{|v| puts v} if ENV['DEBUG']
    raise ConfigError.new("Unable to start due to invalid settings")
  end
  config
end

Instance Method Details

#optional(name, default) ⇒ Object



39
40
41
# File 'lib/ficus.rb', line 39

def optional(name, default)
  self.send("#{name}=", default) if self.send(name).nil?
end

#required(name) ⇒ Object



43
44
45
46
# File 'lib/ficus.rb', line 43

def required(name)
  prefix = self.parent ? "#{self.parent}." : nil
  Ficus.log "[ERR] Option #{prefix}#{name} is not defined" if self.send(name).nil?
end

#section(name, args = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/ficus.rb', line 28

def section(name, args = {}, &block)
  section = self.send(name)
  if section.nil?
    level = args[:optional] ? 'WARN' : 'ERR'
    Ficus.log "[#{level}] Section #{name} is not defined"
  else
    section.parent = self.parent ? "#{self.parent}.#{name}" : name
    section.instance_eval &block if block_given?
  end
end