Class: Mutaconf::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mutaconf/config.rb

Defined Under Namespace

Classes: Error, JsonParser, YamlParser

Constant Summary collapse

DEFAULT_FORMAT =
:ruby

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}
  @file = file
  @parser = options[:parser]
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#parserObject (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

Yields:

Raises:



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

  file_options = args.last.kind_of?(Hash) ? args.pop : {}
  options = [ :read, :load, :parser, :parser_options ].inject({}){ |memo,o| memo[o] = file_options.delete o; memo }
  file_options.delete :all

  parser = if options[:parser]
    options[:parser]
  elsif file_options[:format] == :yaml
    YamlParser.new options[:parser_options]
  elsif file_options[:format] == :json
    JsonParser.new options[:parser_options]
  end

  file = find_file *(args.push file_options)
  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 options[:read]
  config.load if options[: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

  options = args.last.kind_of?(Hash) ? args.pop : {}

  format = selected_format options
  all = options[:all]

  found = []

  args.each do |name|

    # TODO: check user-supplied locations for duplicates
    selected_locations(options).each do |location|

      prefix = dot?(location, options) ? '.' : nil
      path = location[:path] || File.expand_path(Dir.pwd)

      selected_types(options).each do |type|

        build_suffixes(format, type, options).each do |suffix|

          file = File.expand_path(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

#contentsObject



21
22
23
# File 'lib/mutaconf/config.rb', line 21

def contents
  @contents ||= @parser ? @parser.parse(raw) : raw
end

#rawObject



17
18
19
# File 'lib/mutaconf/config.rb', line 17

def raw
  @raw ||= File.read @file
end