Sha256: 3559a5e06a1406b3d793dcf97d504a45a46fa7188a25df444f819eeaa5e41ad8

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'ostruct'
module RailsConfig
  class Options < OpenStruct

    def empty?
      marshal_dump.empty?
    end

    def add_source!(source)
      @config_sources ||= []
      @config_sources << source
    end

    def load!
      reload!
    end

    # look through all our sources and rebuild the configuration
    def reload!
      conf = {}
      @config_sources.each do |source|
        source_conf = source.load

        if conf.empty?
          conf = source_conf
        else
          DeepMerge.deep_merge!(source_conf, conf, :preserve_unmergeables => false)
        end
      end

      # swap out the contents of the OStruct with a hash (need to recursively convert)
      marshal_load(__convert(conf).marshal_dump)

      return self
    end

    alias :load! :reload!

    protected

    # Recursively converts Hashes to Options (including Hashes inside Arrays)
    def __convert(h) #:nodoc:
      s = self.class.new
      unless h.is_a?(Hash)
        require 'ruby-debug';debugger
      end
      h.each do |k, v|
        s.new_ostruct_member(k)
        if v.is_a?(Hash)
          s.send( (k+'=').to_sym, __convert(v))
        elsif v.is_a?(Array)
          converted_array = v.collect { |e| e.instance_of?(Hash) ? __convert(e) : e }
          s.send("#{k}=".to_sym, converted_array)
        else
          s.send("#{k}=".to_sym, v)
        end
      end
      s
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_config-0.1.4 lib/rails_config/options.rb
rails_config-0.1.3 lib/rails_config/options.rb