Class: Contracts::Formatters::TypeAST

Inherits:
Object
  • Object
show all
Defined in:
lib/yard-contracts/formatters.rb

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ TypeAST

Returns a new instance of TypeAST


145
146
147
# File 'lib/yard-contracts/formatters.rb', line 145

def initialize(type)
  @type = type
end

Instance Method Details

#array_type(array) ⇒ Object

Formats Array type.


172
173
174
175
# File 'lib/yard-contracts/formatters.rb', line 172

def array_type(array)
  # This works because Ast inherits from Array.
  array.map{ |v| Contracts::Formatters::InspectWrapper.new(type(v)) }.inspect
end

#hash_type(hash) ⇒ Object

Formats Hash type.


161
162
163
164
165
166
167
168
169
# File 'lib/yard-contracts/formatters.rb', line 161

def hash_type(hash)
  # Ast inherits from Array not Hash so we have to enumerate :assoc nodes
  # which are key value pairs of the Hash and build from that.
  result = {}
  hash.each do |h|
    result[h[0].jump(:label).last.to_sym] = Contracts::Formatters::InspectWrapper.new(type(h[1]))
  end
  result
end

#type(type = @type) ⇒ Object

Formats any type of type.


150
151
152
153
154
155
156
157
158
# File 'lib/yard-contracts/formatters.rb', line 150

def type(type = @type)
  if type.type == :hash
    hash_type(type)
  elsif type.type == :array
    array_type(type)
  else
    type.source
  end
end