Class: Configulations

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = "./config") ⇒ Configulations

Returns a new instance of Configulations.



11
12
13
14
15
16
17
18
# File 'lib/configulations.rb', line 11

def initialize(root="./config")
  @root                 = File.expand_path(root)
  @supported_extensions = [:yml, :yaml, :js, :json]
  @environments         = ["RAILS_ENV", "RACK_ENV", "APP_ENV"]
  @properties           = MagicHash.new
  find_properties(config_files_at_dir(@root))
  @properties.objectify
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message_name, *message_arguments, &optional_block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/configulations.rb', line 76

def method_missing(message_name, *message_arguments, &optional_block)
  message = message_name.to_s.strip.gsub(/-/,"_")
  if message =~ /=/
    @properties[message.gsub(/=/,"").to_sym] = message_arguments.flatten.first
    return
  elsif message =~ /\?/
    return !!(@properties[message.gsub(/\?/,"").to_sym])
  else
    return @properties[message.to_sym] if @properties.has_key? message.to_sym
  end

  super message_name, *message_arguments, &optional_block
end

Instance Attribute Details

#environmentsObject

Returns the value of attribute environments.



9
10
11
# File 'lib/configulations.rb', line 9

def environments
  @environments
end

#propertiesObject

Returns the value of attribute properties.



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

def properties
  @properties
end

#rootObject

Returns the value of attribute root.



6
7
8
# File 'lib/configulations.rb', line 6

def root
  @root
end

#supported_extensionsObject

Returns the value of attribute supported_extensions.



8
9
10
# File 'lib/configulations.rb', line 8

def supported_extensions
  @supported_extensions
end

Instance Method Details

#config_files_at_dir(dir) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/configulations.rb', line 67

def config_files_at_dir(dir)
  ( config_files = glob_directory_against_supported_extensions(dir.to_s) ).reject do |file|
    ext  = File.extname(file)
    base = File.basename(file, ext)
    parent_config = file.gsub(/\/#{base}#{ext}/, ext)
    config_files.include?(parent_config)
  end
end

#environmental_override?(base) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/configulations.rb', line 20

def environmental_override?(base)
  !@environments.select{|ev| base.downcase == (ENV[ev] || "").downcase}.empty?
end

#ext_glob_stringObject



52
53
54
# File 'lib/configulations.rb', line 52

def ext_glob_string
  supported_extensions.map{|x|x.to_s}.join(",")
end

#find_properties(config_files, props = @properties, parent = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/configulations.rb', line 24

def find_properties(config_files, props=@properties, parent=nil)
  return if config_files.empty?
  file        = config_files.shift
  ext         = File.extname(file)
  base        = File.basename(file, ext)
  parser      = parser_for_extname(ext)
  config_data = parser.send(:load, File.read(file))

  if parent
    props.merge!(config_data) if environmental_override?(base)
    props[parent] = config_data if props[parent]
  end

  props[base] = config_data

  dir = "#{File.dirname(file)}/#{base}"
  if File.exists?(dir) and File.directory?(dir)
    child_configs = glob_directory_against_supported_extensions(dir)
    find_properties(child_configs, props[base], base)
  end

  find_properties(config_files, props, parent)
end

#glob_directory_against_supported_extensions(dir) ⇒ Object



48
49
50
# File 'lib/configulations.rb', line 48

def glob_directory_against_supported_extensions(dir)
  Dir.glob("#{dir}/*.{#{ext_glob_string}}")
end

#parser_for_extname(extname) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/configulations.rb', line 56

def parser_for_extname(extname)
  case extname
  when /\.js(?:on)?/i
    return JSON
  when /\.ya?ml/i
    return YAML
  else
    raise "Only files ending in .js, .json, .yml, .yaml have parsers at the moment."
  end
end