Class: Typedocs::BlockSpec

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name) ⇒ BlockSpec

Returns a new instance of BlockSpec.

Raises:



3
4
5
6
7
# File 'lib/typedocs/block_spec.rb', line 3

def initialize(type, name)
  raise ArgumentError unless [:req, :opt, :none].include?(type)
  @name = name
  @type = type
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/typedocs/block_spec.rb', line 8

def name
  @name
end

Instance Method Details

#error_message_for(block) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/typedocs/block_spec.rb', line 19

def error_message_for(block)
  raise ArgumentError if valid?(block)
  case @type
  when :req
    "Block not given"
  when :none
    "Block not allowed"
  else
    raise 'maybe typedocs bug'
  end
end

#to_sourceObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/typedocs/block_spec.rb', line 30

def to_source
  n = name
  case @type
  when :req
    "&#{n}"
  when :opt
    "?&#{n}"
  when :none
    ''
  else
    raise "Invalid type: #{@type}"
  end
end

#to_source_with_arrowObject



43
44
45
46
47
48
49
# File 'lib/typedocs/block_spec.rb', line 43

def to_source_with_arrow
  if @type == :none
    ''
  else
    "#{to_source} -> "
  end
end

#valid?(block) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'lib/typedocs/block_spec.rb', line 9

def valid?(block)
  case block
  when nil
    return @type == :opt || @type == :none
  when Proc
    return @type == :opt || @type == :req
  else
    raise 'maybe typedocs bug'
  end
end