Class: SafeMe::TypeSafer

Inherits:
Object
  • Object
show all
Defined in:
lib/safe-me/type_safer.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ TypeSafer

Returns a new instance of TypeSafer.



9
10
11
12
# File 'lib/safe-me/type_safer.rb', line 9

def initialize &block
  @types = []
  instance_eval &block
end

Instance Method Details

#arguments(*types) ⇒ Object



28
29
30
31
32
# File 'lib/safe-me/type_safer.rb', line 28

def arguments *types
  types.each do |t|
    argument t
  end
end

#check(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/safe-me/type_safer.rb', line 14

def check *args
  tmp = args
  if @types.last.kind_of?(VarArgs)
    tmp = args.slice(0, @types.size-1) + [args.slice(@types.size-1,args.size)]
  end
  tmp.size.times do |i|
    raise ArgumentError.new("for argument #{i+1} expected type #{@types[i]}") unless  @types[i].type_of?(tmp[i])
  end
end

#nilable(type) ⇒ Object



38
39
40
# File 'lib/safe-me/type_safer.rb', line 38

def nilable type
  NilableType.new type
end

#responds_to(*methods) ⇒ Object



42
43
44
# File 'lib/safe-me/type_safer.rb', line 42

def responds_to *methods
  RespondsTo.new *methods
end

#uncheckedObject



46
47
48
# File 'lib/safe-me/type_safer.rb', line 46

def unchecked
  UncheckedType.instance
end