Class: Mutaconf::Config
- Inherits:
-
Object
- Object
- Mutaconf::Config
- Defined in:
- lib/mutaconf/config.rb
Defined Under Namespace
Classes: Error, JsonParser, YamlParser
Constant Summary collapse
- DEFAULT_FORMAT =
:ruby
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
Instance Method Summary collapse
- #contents ⇒ Object
-
#initialize(file, options = {}) ⇒ Config
constructor
A new instance of Config.
- #raw ⇒ Object
Constructor Details
#initialize(file, options = {}) ⇒ Config
Returns a new instance of Config.
12 13 14 15 |
# File 'lib/mutaconf/config.rb', line 12 def initialize file, = {} @file = file @parser = [:parser] end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/mutaconf/config.rb', line 7 def file @file end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
7 8 9 |
# File 'lib/mutaconf/config.rb', line 7 def parser @parser end |
Class Method Details
.find(*args) {|config| ... } ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mutaconf/config.rb', line 25 def self.find *args = args.last.kind_of?(Hash) ? args.pop : {} = [ :read, :load, :parser, :parser_options ].inject({}){ |memo,o| memo[o] = .delete o; memo } .delete :all parser = if [:parser] [:parser] elsif [:format] == :yaml YamlParser.new [:parser_options] elsif [:format] == :json JsonParser.new [:parser_options] end file = find_file *(args.push ) return nil unless file raise Error, "Parser must respond to :parse" if parser and !parser.respond_to?(:parse) config = Config.new file, parser: parser config.raw if [:read] config.load if [:load] yield config if block_given? config end |
.find_file(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mutaconf/config.rb', line 51 def self.find_file *args = args.last.kind_of?(Hash) ? args.pop : {} format = selected_format all = [:all] found = [] args.each do |name| # TODO: check user-supplied locations for duplicates selected_locations().each do |location| prefix = dot?(location, ) ? '.' : nil path = location[:path] || File.(Dir.pwd) selected_types().each do |type| build_suffixes(format, type, ).each do |suffix| file = File.(File.join(path, "#{prefix}#{name}#{suffix}")) if File.file? file return file unless all found << file end end end end end all ? found : nil end |
Instance Method Details
#contents ⇒ Object
21 22 23 |
# File 'lib/mutaconf/config.rb', line 21 def contents @contents ||= @parser ? @parser.parse(raw) : raw end |
#raw ⇒ Object
17 18 19 |
# File 'lib/mutaconf/config.rb', line 17 def raw @raw ||= File.read @file end |