Module: Rbcli::Configurable

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbcli/components/core/configurate.rb', line 21

def self.included klass
  # We dynamically add two methods to the module: one that runs other methods dynamially, and one that
  # displays a reasonable message if a method is missing
  klass.singleton_class.class_eval do
    define_method :rbcli_private_running_method do |&block|
      @self_before_instance_eval = eval 'self', block.binding
      instance_eval &block
    end

    define_method :method_missing do |method, *args, &block|
      raise Rbcli::ConfigurateError.new "Invalid Configurate.#{self.name.split('::')[-1].downcase} method called: `#{method}` in file #{File.expand_path caller[0]}"
    end
  end


  # This will dynamically create the configurate block based on the class name.
  # For example, if the class name is 'Me', then the resulting block is `Configurate.me`
  name = klass.name.split('::')[-1]
  Rbcli::Configurate.singleton_class.class_eval do
    define_method name.downcase.to_sym do |&block|
      mod = self.const_get name
      on_declare = mod.instance_variable_get('@on_declare')
      on_declare.call if on_declare.is_a?(Proc)
      begin
        mod.rbcli_private_running_method &block
      rescue Rbcli::ConfigurateError => e
        Rbcli.log.fatal 'Rbcli Configuration Error: ' + e.message
        Rbcli::exit 255
      end
    end
  end
end