Class: Ficus

Inherits:
RecursiveOpenStruct
  • Object
show all
Defined in:
lib/ficus/dsl.rb,
lib/ficus/loader.rb,
lib/ficus/exceptions.rb

Defined Under Namespace

Classes: ConfigError, NoSection

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/ficus/loader.rb', line 3

def errors
  @errors
end

.logger=(value) ⇒ Object

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



3
4
5
# File 'lib/ficus/loader.rb', line 3

def logger=(value)
  @logger = value
end

.verboseObject

Returns the value of attribute verbose.



3
4
5
# File 'lib/ficus/loader.rb', line 3

def verbose
  @verbose
end

.warningsObject

Returns the value of attribute warnings.



3
4
5
# File 'lib/ficus/loader.rb', line 3

def warnings
  @warnings
end

Class Method Details

.error(msg) ⇒ Object



16
17
18
# File 'lib/ficus/loader.rb', line 16

def error(msg)
  @errors << msg
end

.load(file, &block) ⇒ Object

Load the configuration file and validate.

Raises:



6
7
8
9
10
11
12
13
14
# File 'lib/ficus/loader.rb', line 6

def load(file, &block)
  @errors, @warnings, @config = [], [], nil
  config(file).instance_eval(&block) if block_given?

  warnings.each { |e| logger.warn e }
  errors.each { |e| logger.error e }
  raise ConfigError.new('Unable to start due to invalid settings') if errors.size > 0
  config(file)
end

.warning(msg) ⇒ Object



20
21
22
# File 'lib/ficus/loader.rb', line 20

def warning(msg)
  @warnings << msg
end

Instance Method Details

#optional(name, default) ⇒ Object



28
29
30
# File 'lib/ficus/dsl.rb', line 28

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

#required(name) ⇒ Object



32
33
34
35
# File 'lib/ficus/dsl.rb', line 32

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

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



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/ficus/dsl.rb', line 2

def section(name, args = {}, &block)
  sections(name).each do |s|
    s.parent = self.parent ? "#{self.parent}.#{name}" : name
    s.instance_eval(&block) if block_given?
  end
rescue NoSection
  if args[:optional] == true
    Ficus.warning("Section #{name} is not defined")
  else
    Ficus.error("Section #{name} is not defined")
  end
end

#sections(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ficus/dsl.rb', line 15

def sections(name)
  if name == :all
    sections(/.*/)
  elsif name.is_a? Regexp
    matches = self.marshal_dump.keys
    matches.map { |k| self.send(k) unless k == :parent }.compact!
  else
    s = self.send(name)
    raise NoSection.new if s.nil?
    [s]
  end
end