aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/configurable.rb
blob: 890f465ce165bd7785f13ba82a596ae306c2681e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require "active_support/concern"

module ActiveSupport
  module Configurable
    extend ActiveSupport::Concern

    module ClassMethods
      def get_config
        module_parts = name.split("::")
        modules = [Object]
        module_parts.each {|name| modules.push modules.last.const_get(name) }
        modules.reverse_each do |mod|
          return mod.const_get(:DEFAULT_CONFIG) if const_defined?(:DEFAULT_CONFIG)
        end
        {}
      end
      
      def config
        self.config = get_config unless @config
        @config
      end

      def config=(hash)
        @config = ActiveSupport::OrderedOptions.new
        hash.each do |key, value|
          @config[key] = value
        end
      end
    end

    def config
      self.class.config
    end
  end
end