Module: Rbcli::Configurable

Included in:
Rbcli::Configurate::Hooks, Rbcli::Configurate::Me, Rbcli::Configurate::Storage
Defined in:
lib/rbcli/configuration/configurate.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
# File 'lib/rbcli/configuration/configurate.rb', line 46

def self.included klass
  name = klass.name.split('::')[-1]

  # 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|
      msg = "Invalid Configurate.#{self.name.split('::')[-1].downcase} method called: `#{method}` in file #{File.expand_path caller[0]}"
      Rbcli::log.fatal {msg}
      raise Exception.new msg
    end
  end

  # This will dynamically create the configuate block based on the class name.
  # For example, if the class name is 'Me', then the resulting block is `Confiugrate.me`
  Rbcli::Configurate.singleton_class.class_eval do
    define_method name.downcase.to_sym do |&block|
      mod = self.const_get name
      mod.rbcli_private_running_method &block
    end
  end
end