lib/yard-contracts/formatters.rb in yard-contracts-0.1.4 vs lib/yard-contracts/formatters.rb in yard-contracts-0.1.5
- old
+ new
@@ -1,99 +1,10 @@
require 'contracts/builtin_contracts'
module Contracts
# A namespace for classes related to formatting.
module Formatters
- # Used to format contracts for the `Expected:` field of error output.
- class Expected
- def initialize(contract, full = true)
- @contract, @full = contract, full
- end
-
- # Formats any type of Contract.
- def contract(contract = @contract)
- if contract.is_a?(Hash)
- hash_contract(contract)
- elsif contract.is_a?(Array)
- array_contract(contract)
- else
- InspectWrapper.new(contract, @full)
- end
- end
-
- # Formats Hash contracts.
- def hash_contract(hash)
- @full = true
- hash.inject({}) do |repr, (k, v)|
- repr.merge(k => InspectWrapper.new(contract(v), @full))
- end.inspect
- end
-
- # Formats Array contracts.
- def array_contract(array)
- @full = true
- array.map { |v| InspectWrapper.new(contract(v), @full) }.inspect
- end
- end
-
- # A wrapper class to produce correct inspect behaviour for different
- # contract values - constants, Class contracts, instance contracts etc.
- class InspectWrapper
- def initialize(value, full = true)
- @value, @full = value, full
- end
-
- # Inspect different types of contract values.
- # Contracts module prefix will be removed from classes.
- # Custom to_s messages will be wrapped in round brackets to differentiate
- # from standard Strings.
- # Primitive values e.g. 42, true, nil will be left alone.
- def inspect
- return '' unless full?
- return @value.inspect if empty_val?
- return @value.to_s if plain?
- return delim(@value.to_s) if useful_to_s?
- @value.inspect.gsub(/^Contracts::/, '')
- end
-
- def delim(value)
- @full ? "(#{value})" : "#{value}"
- end
-
- # Eliminates eronious quotes in output that plain inspect includes.
- def to_s
- inspect
- end
-
- private
-
- def empty_val?
- @value.nil? || @value == ''
- end
-
- def full?
- @full ||
- @value.is_a?(Hash) || @value.is_a?(Array) ||
- (!plain? && useful_to_s?)
- end
-
- def plain?
- # Not a type of contract that can have a custom to_s defined
- !@value.is_a?(CallableClass) && @value.class != Class
- end
-
- def useful_to_s?
- # Useless to_s value or no custom to_s behavious defined
- @value.to_s != '' &&
- if @value.class == Class # It's a class contract
- @value.to_s != @value.name
- else # It's an instance contract
- !@value.to_s.match(/#\<\w+:.+\>/)
- end
- end
- end
-
class TypesAST
def initialize(types)
@types = types[0..-2]
end
@@ -168,20 +79,20 @@
# 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]))
+ Contracts::Formatters::InspectWrapper.create(type(h[1]))
end
result
end
# Formats Array type.
def array_type(array)
# This works because Ast inherits from Array.
array.map do |v|
- Contracts::Formatters::InspectWrapper.new(type(v))
+ Contracts::Formatters::InspectWrapper.create(type(v))
end.inspect
end
end
class ParamContracts
@@ -225,11 +136,11 @@
@named_type = type = []
end
named_count = 1
end
- type = Contracts::Formatters::InspectWrapper.new(type)
+ type = Contracts::Formatters::InspectWrapper.create(type)
desc = Contracts::Formatters::Expected.new(con, false).contract
# The pluses are to escape things like curly brackets
desc = "#{desc}".empty? ? '' : "+#{desc}+"
s << [param, type, desc]
i += 1
@@ -238,10 +149,10 @@
end
def return
type, type_ast = @result
con = get_contract_value(type)
- type = Contracts::Formatters::InspectWrapper.new(
+ type = Contracts::Formatters::InspectWrapper.create(
TypeAST.new(type_ast).type
)
desc = Contracts::Formatters::Expected.new(con, false).contract
desc = "#{desc}".empty? ? '' : "+#{desc}+"
[type, desc]