Class: SafeMe::TypeSafer
- Inherits:
-
Object
- Object
- SafeMe::TypeSafer
- Defined in:
- lib/safe-me/type_safer.rb
Instance Method Summary collapse
- #arguments(*types) ⇒ Object
- #check(*args) ⇒ Object
-
#initialize(&block) ⇒ TypeSafer
constructor
A new instance of TypeSafer.
- #nilable(type) ⇒ Object
- #responds_to(*methods) ⇒ Object
- #unchecked ⇒ Object
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 |
#unchecked ⇒ Object
46 47 48 |
# File 'lib/safe-me/type_safer.rb', line 46 def unchecked UncheckedType.instance end |