Module: Configurations::Configurable

Extended by:
Configurable
Included in:
Configurations, Configurable
Defined in:
lib/configurations/configurable.rb

Overview

Module configurable provides the API of configurations

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#included(base) ⇒ Object

Once included, Configurations installs three methods in the host module: configure, configuration_defaults and configurable



12
13
14
15
16
17
18
19
20
21
# File 'lib/configurations/configurable.rb', line 12

def included(base)
  install_configure_in(base)
  base.instance_eval do
    extend ClassMethods

    # call configuration_mutex once to initialize the value
    #
    initialize_configuration!
  end
end

#install_configure_in(base) ⇒ Object

Installs #configure in base, and makes sure that it will instantiate configuration as a subclass of the host module



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/configurations/configurable.rb', line 34

def install_configure_in(base)
  base.instance_eval <<-EOF
    # Configuration class for host module
    #
    #{base.name}::Configuration = Class.new(Configurations::Configuration)

    # The central configure method
    # @params [Proc] block the block to configure host module with
    # @raise [ArgumentError] error when not given a block
    # @example Configure a configuration
    #   MyGem.configure do |c|
    #     c.foo = :bar
    #   end
    #
    def self.configure(&block)
      semaphore.synchronize do
        fail ArgumentError, "configure needs a block" unless block_given?
        include_configuration_type!(#{base.name}::Configuration)

        set_configuration!(&block)
      end
    end

    # A reader for Configuration
    #
    def configuration
      semaphore.synchronize do
        return @configuration if @configuration

        if @configuration_defaults
          include_configuration_type!(#{base.name}::Configuration)
          set_configuration! { }
        end
      end
    end


    private

    # Sets the configuration instance variable
    #
    def self.set_configuration!(&block)
      @configuration = #{base.name}::Configuration.__new__(
                                                  configuration_options,
                                                  &block
                                                )
    end

    @semaphore = Mutex.new
    def self.semaphore
      @semaphore
    end

  EOF
end

#underscore_camelized(string) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/configurations/configurable.rb', line 23

def underscore_camelized(string)
  string.gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end