Module: Starter::Extensions::Module

Defined in:
lib/starter/extensions/module.rb

Class Method Summary collapse

Class Method Details

.basename(mod) ⇒ Object



8
9
10
# File 'lib/starter/extensions/module.rb', line 8

def self.basename(mod)
  mod.name.split('::').last
end

.create_instance_methods(mod) ⇒ Object

For every locally-defined class method, create an instance method of the same name which operates on the instance. This allows you to create Modules which can be used as mixins, but the methods are also available on the module itself, in case a user does not want to include the module.



31
32
33
34
35
36
37
38
39
# File 'lib/starter/extensions/module.rb', line 31

def self.create_instance_methods(mod)
  self.local_methods(mod).each do |method_name|
    mod.module_eval "      def \#{method_name}(*args)\n        \#{mod}.\#{method_name}(self, *args)\n      end\n    METHOD\n  end\nend\n", __FILE__, __LINE__

.local_instance_methods(mod) ⇒ Object



19
20
21
22
23
24
# File 'lib/starter/extensions/module.rb', line 19

def self.local_instance_methods(mod)
  mod.public_instance_methods.select do |m|
    owner = mod.instance_method(m).owner
    owner == mod
  end
end

.local_methods(mod) ⇒ Object



12
13
14
15
16
17
# File 'lib/starter/extensions/module.rb', line 12

def self.local_methods(mod)
  mod.public_methods.select do |m|
    owner = mod.method(m).owner
    owner == mod.singleton_class || owner == mod
  end
end