Class: Typedocs::MethodSpec::Single

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args_spec, block_spec, retval_spec) ⇒ Single

Returns a new instance of Single.



38
39
40
41
42
43
44
45
# File 'lib/typedocs/method_spec.rb', line 38

def initialize(args_spec, block_spec, retval_spec)
  Typedocs.ensure_klass(args_spec, Typedocs::ArgumentsSpec)
  Typedocs.ensure_klass(block_spec, Typedocs::BlockSpec)
  Typedocs.ensure_klass(retval_spec, Typedocs::TypeSpec)
  @arguments_spec = args_spec
  @block_spec = block_spec
  @retval_spec = retval_spec
end

Instance Attribute Details

#arguments_specObject (readonly)

Returns the value of attribute arguments_spec.



47
48
49
# File 'lib/typedocs/method_spec.rb', line 47

def arguments_spec
  @arguments_spec
end

#block_specObject (readonly)

Returns the value of attribute block_spec.



48
49
50
# File 'lib/typedocs/method_spec.rb', line 48

def block_spec
  @block_spec
end

#retval_specObject (readonly)

Returns the value of attribute retval_spec.



49
50
51
# File 'lib/typedocs/method_spec.rb', line 49

def retval_spec
  @retval_spec
end

Instance Method Details

#call_with_validate(method, *args, &block) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/typedocs/method_spec.rb', line 51

def call_with_validate(method, *args, &block)
  validate_args args
  validate_block block
  ret = method.call *args, &block
  validate_retval ret
  ret
end

#to_sourceObject



79
80
81
82
83
84
85
# File 'lib/typedocs/method_spec.rb', line 79

def to_source
  s = ''
  s << arguments_spec.to_source
  s << " -> " unless arguments_spec.empty?
  s << block_spec.to_source_with_arrow
  s << "#{retval_spec.to_source}"
end

#validate_args(args) ⇒ Object



64
65
66
# File 'lib/typedocs/method_spec.rb', line 64

def validate_args(args)
  raise Typedocs::ArgumentError, arguments_spec.error_message_for(args) unless arguments_spec.valid?(args)
end

#validate_block(block) ⇒ Object



68
69
70
71
72
73
# File 'lib/typedocs/method_spec.rb', line 68

def validate_block(block)
  raise Typedocs::BlockError, "Cant accept block" if !block_spec && block
  if block_spec
    raise Typedocs::BlockError, block_spec.error_message_for(block) unless block_spec.valid?(block)
  end
end

#validate_caller(args, block) ⇒ Object



59
60
61
62
# File 'lib/typedocs/method_spec.rb', line 59

def validate_caller(args, block)
  validate_args args
  validate_block block
end

#validate_retval(ret) ⇒ Object



75
76
77
# File 'lib/typedocs/method_spec.rb', line 75

def validate_retval(ret)
  raise Typedocs::RetValError, retval_spec.error_message_for(ret) unless retval_spec.valid?(ret)
end