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.



151
152
153
# File 'lib/yard-contracts/formatters.rb', line 151

def initialize(type)
  @type = type
end

Instance Method Details

#array_type(array) ⇒ Object

Formats Array type.



179
180
181
182
183
184
# File 'lib/yard-contracts/formatters.rb', line 179

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

#hash_type(hash) ⇒ Object

Formats Hash type.



167
168
169
170
171
172
173
174
175
176
# File 'lib/yard-contracts/formatters.rb', line 167

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.



156
157
158
159
160
161
162
163
164
# File 'lib/yard-contracts/formatters.rb', line 156

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