Module: ConfigurationBlocks::ClassMethods

Defined in:
lib/configuration-blocks/core.rb

Overview

This module contains methods that will extend the class.

Instance Method Summary collapse

Instance Method Details

#configuration_block(base = self, &block) ⇒ Object

Returns configuration block.

Parameters:

  • base (Object, Symbol) (defaults to: self)

    base object which delegators will point to (defaults to object on which this method has been called). If symbol is given, then it should contain the name of a method that will be called on current object.

Returns:

  • (Object)

    result of evaluating the given block within context of configuration module



70
71
72
73
# File 'lib/configuration-blocks/core.rb', line 70

def configuration_block(base = self, &block)
  @cb_conf_module ||= configuration_module(base)
  configuration_block_core(@cb_conf_module, &block)
end

#configuration_block_delegate(*methods) ⇒ nil Also known as: configuration_method, configuration_methods, settings_method

This DSL method is intended to be used in a class or module to indicate which methods should be delegated.

Parameters:

  • methods (Array<Symbol,String>)

    list of method names

Returns:

  • (nil)


119
120
121
122
123
# File 'lib/configuration-blocks/core.rb', line 119

def configuration_block_delegate(*methods)
  methods.flatten.each { |m| cf_block_delegators.add(m.to_sym) }
  @cb_conf_module = nil if @cb_conf_module
  nil
end

#configuration_module(base = self) ⇒ Module

Creates and returns anonymous module containing delegators that point to methods from a class this module is included in or the given base.

Parameters:

  • base (Object, Symbol) (defaults to: self)

    base object which delegators will point to (defaults to object on which this method has been called). If symbol is given, then it should contain the name of a method that will be called on current object.

Returns:

  • (Module)

    anonymous module with proxy module methods delegating actions to base object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/configuration-blocks/core.rb', line 84

def configuration_module(base = self)
  Module.new.tap do |cm|
    delegators = get_configuration_methods
    base = send(base) if base.is_a?(Symbol)
    cm.extend Module.new {
      delegators.each do |method|
        module_eval do
          define_method(method) do |*args|
            base.send(method, *args)
          end
        end
      end
    }
  end
end

#get_configuration_methods(local_only = false) ⇒ Array<Symbol>

Gets all method names known to configuration engine.

Parameters:

  • local_only (Boolean) (defaults to: false)

    optional flag that if set, causes only methods added by current class or module to be listed.

Returns:

  • (Array<Symbol>)

    delegated method names



106
107
108
109
110
111
112
# File 'lib/configuration-blocks/core.rb', line 106

def get_configuration_methods(local_only = false)
  all_delegators = singleton_class.send(:cf_block_delegators) + cf_block_delegators
  return all_delegators if local_only
  ancestors.each_with_object(all_delegators) do |ancestor, all|
    all.merge(ancestor.send(__method__, true)) if ancestor.respond_to?(__method__)
  end
end