class Mutaconf::Config
Constants
- DEFAULT_FORMAT
- FORMATS
- LOCATIONS
- TYPES
Attributes
file[R]
parser[R]
Public Class Methods
find(*args) { |config| ... }
click to toggle source
# 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)
click to toggle source
# 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
new(file, options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 12 def initialize file, options = {} @file = file @parser = options[:parser] end
Private Class Methods
build_filters(criteria, defaults)
click to toggle source
# File lib/mutaconf/config.rb, line 144 def self.build_filters criteria, defaults h = criteria.kind_of?(Hash) ? criteria : { only: criteria.kind_of?(Array) ? criteria : [ criteria ].compact } h[:only] = defaults if h[:only].nil? or h[:only].empty? h[:only] = [ h[:only] ].compact unless h[:only].kind_of?(Array) h[:except] = [ h[:except] ].compact unless h[:except].kind_of?(Array) h end
build_suffixes(format, type, options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 116 def self.build_suffixes format, type, options = {} suffixes = type[:suffix] if suffixes == :format and FORMATS[format] exts = FORMATS[format][:extension] exts = [ exts ] unless exts.kind_of? Array suffixes = exts.collect{ |ext| ".#{ext}" } end suffixes = [ suffixes ] unless suffixes.kind_of? Array suffixes end
dot?(location, options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 108 def self.dot? location, options = {} (options.key?(:dot) ? options[:dot] : true) and (location.key?(:dot) ? location[:dot] : true) end
filter(a, criteria, defaults)
click to toggle source
# File lib/mutaconf/config.rb, line 139 def self.filter a, criteria, defaults filters = build_filters criteria, defaults a.select{ |o| filters[:only].include? o[:name] }.reject{ |o| filters[:except].include? o[:name] } end
selected_format(options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 112 def self.selected_format options = {} options.key?(:format) ? options[:format] : DEFAULT_FORMAT end
selected_locations(options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 133 def self.selected_locations options = {} locations = filter LOCATIONS, options[:locations] || options[:location], LOCATIONS.collect{ |l| l[:name] } locations.delete_if{ |l| l[:name] == :cwd } if locations.any?{ |l| l[:name] != :cwd && File.expand_path(Dir.pwd) == File.expand_path(l[:path]) } locations end
selected_types(options = {})
click to toggle source
# File lib/mutaconf/config.rb, line 129 def self.selected_types options = {} filter TYPES, options[:types] || options[:type], TYPES.collect{ |t| t[:name] } end
Public Instance Methods
contents()
click to toggle source
# File lib/mutaconf/config.rb, line 21 def contents @contents ||= @parser ? @parser.parse(raw) : raw end
raw()
click to toggle source
# File lib/mutaconf/config.rb, line 17 def raw @raw ||= File.read @file end