Module: MPatch::Include::Module

Included in:
MPatch
Defined in:
lib/mpatch/module.rb

Instance Method Summary collapse

Instance Method Details

#alias_instance_methods_from(class_name, *sym_names) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/mpatch/module.rb', line 83

def alias_instance_methods_from class_name, *sym_names

  method_names= sym_names.map{|e|e.to_s}
  method_names= class_name.instance_methods if method_names.empty?

  method_names.each do |sym_name|
    self.__send__ :define_method, sym_name, class_name.instance_method(sym_name)
  end

end

#alias_singleton_methods_from(class_name, *sym_names) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mpatch/module.rb', line 64

def alias_singleton_methods_from class_name, *sym_names

  method_names= sym_names.map{|e|e.to_s}
  method_names= class_name.singleton_methods if method_names.empty?

  method_names.each do |sym_name|
    self.define_singleton_method(sym_name) { |*args|

      if class_name.method(sym_name).parameters.empty?
        class_name.method(sym_name).call
      else
        class_name.method(sym_name).call *args
      end

    }
  end

end

#convert_to_hashObject Also known as: conv2hash

convert class instance instance variables into a hash object



99
100
101
102
103
104
105
106
107
108
# File 'lib/mpatch/module.rb', line 99

def convert_to_hash

  tmp_hash= {}
  self.class_variables.each do|var|
    tmp_hash[var.to_s.delete("@@")] = self.class_variable_get(var)
  end

  return tmp_hash

end

#helloObject



94
95
96
# File 'lib/mpatch/module.rb', line 94

def hello
  "hello"
end

#inherited_by(*args) ⇒ Object



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
53
54
55
56
57
58
59
60
61
# File 'lib/mpatch/module.rb', line 25

def inherited_by *args

  if args.empty?
    args.push(::Class)
    args.push(::Module)
  end

  return_array= []
  args.each do |type_name|

    ::ObjectSpace.each_object(type_name) do |candidate|
      begin

        if !return_array.include?(candidate) && candidate != self

          #> can't make subclass of Class so == is enough
          case true

            when self.class == ::Module
              return_array.push candidate if candidate.mixin_ancestors.include?(self)

            when self.class == ::Class
              return_array.push candidate if candidate < self

          end

        end

      rescue ::ArgumentError, ::NoMethodError

      end
    end

  end
  return return_array

end

#mixin_ancestors(include_ancestors = true) ⇒ Object



20
21
22
23
# File 'lib/mpatch/module.rb', line 20

def mixin_ancestors(include_ancestors=true)
  ancestors.take_while {|a| include_ancestors || a != superclass }.
      select {|ancestor| ancestor.instance_of?( ::Module ) }
end

#subclassesObject Also known as: classes

return the module objects direct sub modules



13
14
15
# File 'lib/mpatch/module.rb', line 13

def subclasses
  constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Class}
end

#submodulesObject Also known as: modules

return the module objects direct sub modules



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

def submodules
  constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Module}
end