Class: Semi::Config
- Inherits:
-
Object
- Object
- Semi::Config
- Defined in:
- lib/semi/config.rb
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#validators ⇒ Object
readonly
Returns the value of attribute validators.
Instance Method Summary collapse
-
#initialize(path = nil) ⇒ Config
constructor
A new instance of Config.
- #load(path) ⇒ Object
- #process_file(filename, dictionary) ⇒ Object
Constructor Details
#initialize(path = nil) ⇒ Config
Returns a new instance of Config.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/semi/config.rb', line 11 def initialize(path=nil) @defaults = {} @validators = {} @files = [] @commands = {} if path self.load(path) end end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
9 10 11 |
# File 'lib/semi/config.rb', line 9 def commands @commands end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
6 7 8 |
# File 'lib/semi/config.rb', line 6 def defaults @defaults end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
7 8 9 |
# File 'lib/semi/config.rb', line 7 def files @files end |
#validators ⇒ Object (readonly)
Returns the value of attribute validators.
8 9 10 |
# File 'lib/semi/config.rb', line 8 def validators @validators end |
Instance Method Details
#load(path) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/semi/config.rb', line 22 def load(path) begin data = YAML.load_file(path) rescue Errno::ENOENT $stderr.puts "Unable to read #{path}" exit(1) end if data.key? 'defaults' @defaults = data['defaults'] end if data.key? 'validate' @validators = data['validate'] end if data.key? 'files' @files = data['files'] end if data.key? 'commands' @commands = data['commands'] end return data end |
#process_file(filename, dictionary) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/semi/config.rb', line 49 def process_file(filename, dictionary) if Dir.exist? filename Dir.glob("#{filename}/*").each do |file| process_file(file, dictionary) end elsif File.exist? filename # Read the file and apply @dictionary values contents = File.open(filename, 'r') do |fp| renderer = ERB.new(fp.readlines.join, nil, '%<>-') begin renderer.result(dictionary.instance_eval {binding}) rescue SyntaxError => e $stderr.puts "Error processing ERB expressions in #{filename}" $stderr.puts "ERB reported #{e}" exit 1 end end # Reopen the file and write the rendered contents File.open(filename, 'w') do |fp| fp.write(contents) end else raise RuntimeError, "Unable to find file or directory named #{filename}" end end |