Class: Configulations
- Inherits:
-
Object
- Object
- Configulations
- Defined in:
- lib/configulations.rb
Instance Attribute Summary collapse
-
#include_pattern ⇒ Object
Returns the value of attribute include_pattern.
-
#properties ⇒ Object
Returns the value of attribute properties.
Class Method Summary collapse
Instance Method Summary collapse
- #find_properties ⇒ Object
-
#initialize ⇒ Configulations
constructor
A new instance of Configulations.
- #method_missing(message_name, *message_arguments, &optional_block) ⇒ Object
- #parser_for_extname(extname) ⇒ Object
Constructor Details
#initialize ⇒ Configulations
Returns a new instance of Configulations.
9 10 11 12 |
# File 'lib/configulations.rb', line 9 def initialize @include_pattern = File.(".") << "/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(, *, &optional_block) = .to_s.strip.gsub(/-/,"_") if =~ /=/ @properties[.gsub(/=/,"").to_sym] = .flatten.first return elsif =~ /\?/ return !!(@properties[.gsub(/\?/,"").to_sym]) else return @properties[.to_sym] if @properties.has_key? .to_sym end super , *, &optional_block end |
Instance Attribute Details
#include_pattern ⇒ Object
Returns the value of attribute include_pattern.
7 8 9 |
# File 'lib/configulations.rb', line 7 def include_pattern @include_pattern end |
#properties ⇒ Object
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_properties ⇒ Object
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 |