lib/cupcakinator/base.rb in cupcakinator-1.1.0 vs lib/cupcakinator/base.rb in cupcakinator-1.1.1

- old
+ new

@@ -26,11 +26,12 @@ # @param [Array<Hash>] options # @option options.last [Hash] :dir The directory where the file can be found # @option options.last [Hash] :file The configuration filename # @option options.last [Hash] :method The method used to access the configuration options # @option options.last [Hash] :root_key A key in the top level of the config file that will become the base - # @example Default usage - Foo will load ./config/config.yml into a method named 'config' + # @option options.last [Hash] :allow_missing Allows the config file to be missing, config will return empty Hash + # @example Default usage - Foo will load ./config/config.yml into a method named 'config' and raise on missing # class Foo # include cupcakinator # cupcakinate # end # >> puts Foo.config @@ -65,10 +66,19 @@ # >> puts Foo.config # { :foo => 'bar' } # >> puts Foo.new.config # { :foo => 'bar' } # + # @example with no config file + # + # class Foo + # include cupcakinator + # cupcakinate dir: Rails.root.join('config'), file: 'foo_config.yml', allow_missing: true + # end + # >> puts Foo.config + # {} + # def cupcakinate(*options) if !options.empty? default_options = _cupcakinator_options @cupcakinator_options = default_options.merge(options.last) end @@ -99,10 +109,14 @@ @cupcakinator_config = Cupcakinator::Config.new(yaml_config[rk]) else @cupcakinator_config = Cupcakinator::Config.new(yaml_config) end rescue Errno::ENOENT - raise Cupcakinator::ConfigFileNotFoundError.new(filename, _cupcakinator_options) + if (_cupcakinator_options.allow_missing rescue false) + @cupcakinator_config = Cupcakinator::Config.new({}) + else + raise Cupcakinator::ConfigFileNotFoundError.new(filename, _cupcakinator_options) + end rescue Psych::SyntaxError => e raise Cupcakinator::ConfigFileInvalidError.new(filename, e.message) end \ No newline at end of file