Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/module.rb

Instance Method Summary collapse

Instance Method Details

#method_added(name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/module.rb', line 8

def method_added(name)
  unless @typesafe.nil?
    @typesafe = nil
    alias_method "__typeunsafe_#{name}__", name
    module_eval do
      method_types = {} if method_types.nil?
      method_types[name] = @types
      define_method name do |*args, &block|
        tmp = args
        if method_types[name].last.kind_of?(VarArgs)
          tmp = args.slice(0, method_types[name].size-1) + [args.slice(method_types[name].size-1,args.size)]
        end
        tmp.size.times do |i|
          raise ArgumentError.new("for argument #{i+1} expected type #{method_types[name][i]}") if not tmp[i].kind_of?(method_types[name][i])
        end
        send(:"__typeunsafe_#{name}__",*args,&block)
      end
      alias_method "__typesafe_#{name}__", name
    end
    @types = nil
  end
end

#typesafe(*types) ⇒ Object



31
32
33
34
# File 'lib/module.rb', line 31

def typesafe(*types)
  @typesafe = true
  @types = types
end