Class: Ficus
- Inherits:
-
RecursiveOpenStruct
- Object
- OpenStruct
- RecursiveOpenStruct
- Ficus
- Defined in:
- lib/ficus.rb
Defined Under Namespace
Classes: ConfigError
Constant Summary
Constants inherited from RecursiveOpenStruct
Class Attribute Summary collapse
-
.log(log = {}) ⇒ Object
Returns the value of attribute log.
-
.verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #optional(name, default) ⇒ Object
- #required(name) ⇒ Object
- #section(name, args = {}, &block) ⇒ Object
- #sections(name) ⇒ Object
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 |
.verbose ⇒ Object
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
52 53 54 |
# File 'lib/ficus.rb', line 52 def optional(name, default) self.send("#{name}=", default) if self.send(name).nil? end |
#required(name) ⇒ Object
56 57 58 59 |
# File 'lib/ficus.rb', line 56 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 38 |
# File 'lib/ficus.rb', line 28 def section(name, args = {}, &block) sections(name).each do |section| 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 end |
#sections(name) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ficus.rb', line 40 def sections(name) if name == :all sections(/.*/) elsif name.is_a? String [self.send(name)] elsif name.is_a? Regexp self.marshal_dump.keys.map{|k| self.send(k) unless k == :parent }.compact! end end |