Module: Monkey::Ext::ExtDSL

Included in:
ClassDsl
Defined in:
lib/monkey/ext.rb

Defined Under Namespace

Modules: ClassDsl

Instance Method Summary collapse

Instance Method Details

#alias_core_method(old_name, new_name) ⇒ Object


63
64
65
66
67
# File 'lib/monkey/ext.rb', line 63

def alias_core_method(old_name, new_name)
  new_name = new_name % old_name if new_name.is_a? String
  core_class.send :alias_method, new_name, old_name
  old_name
end

#class_methods(&block) ⇒ Object


80
81
82
# File 'lib/monkey/ext.rb', line 80

def class_methods(&block)
  self::ExtClassMethods.class_eval(&block)
end

#core_class(klass = nil) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/monkey/ext.rb', line 16

def core_class(klass = nil)
  if klass
    @core_class = klass
    klass.send :include, self
    self::ExtClassMethods.extend ClassDsl
    self::ExtClassMethods.core_class @core_class
    @core_class.class_eval <<-EOS
      def method_missing(meth, *args, &blk)
        return super if Monkey::Backend.setup?
        Monkey::Backend.setup
        __send__(meth, *args, &blk)
      end
    EOS
    propagate_include
  end
  @core_class
end

#each_module(&block) ⇒ Object


35
36
37
38
39
# File 'lib/monkey/ext.rb', line 35

def each_module(&block)
  list = []
  ObjectSpace.each_object(Class) { |c| list << c }
  list.each(&block)
end

#expects(*list) ⇒ Object


84
85
86
87
88
# File 'lib/monkey/ext.rb', line 84

def expects(*list)
  list.each do |name|
    Monkey::Ext.expectations[core_class] << name.to_s
  end
end

#feature(name, mode = :instance, &block) ⇒ Object


69
70
71
72
73
74
75
76
77
78
# File 'lib/monkey/ext.rb', line 69

def feature(name, mode = :instance, &block)
  case mode
  when :instance then block.call
  when :class then class_methods(&block)
  when :shared
    feature(name, :instance, &block)
    feature(name, :class, &block)
  else raise ArgumentError, "unkown mode #{mode.inspect}"
  end
end

#included(klass) ⇒ Object


54
55
56
57
# File 'lib/monkey/ext.rb', line 54

def included(klass)
  klass.extend self::ExtClassMethods
  super
end

#propagate_includeObject


46
47
48
49
50
51
52
# File 'lib/monkey/ext.rb', line 46

def propagate_include
  return if core_class.is_a? Class
  each_module do |mod|
    next unless mod < core_class and not mod < self
    mod.send(:include, core_class)
  end
end

#rename_core_method(old_name, new_name) ⇒ Object


59
60
61
# File 'lib/monkey/ext.rb', line 59

def rename_core_method(old_name, new_name)
  core_class.send :undef_method, alias_core_method(old_name, new_name)
end