Class: Configulations

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigulations

Returns a new instance of Configulations.



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

def initialize
  @include_pattern = File.expand_path(".") << "/config/**/*.{yml,json}"
  find_properties
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/configulations.rb', line 45

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

#include_patternObject

Returns the value of attribute include_pattern.



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

def include_pattern
  @include_pattern
end

#propertiesObject

Returns the value of attribute properties.



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

def properties
  @properties
end

Class Method Details

.configure(&blk) ⇒ Object



14
15
16
17
18
19
# File 'lib/configulations.rb', line 14

def self.configure(&blk)
  me = Configulations.new
  blk.call(me)
  me.find_properties
  me
end

Instance Method Details

#find_propertiesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/configulations.rb', line 21

def find_properties
  @properties = {}
  @properties.extend(MagicHash)

  Dir[@include_pattern].each do |file|
    ext = File.extname(file)
    base = File.basename(file, ext)
    parser = parser_for_extname(ext)
    @properties[base]= parser.send(:load, File.read(file))
  end

  @properties.objectify
end

#parser_for_extname(extname) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/configulations.rb', line 35

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