lib/yard-contracts/formatters.rb in yard-contracts-0.1.1 vs lib/yard-contracts/formatters.rb in yard-contracts-0.1.2

- old
+ new

@@ -3,11 +3,11 @@ 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) + def initialize(contract, full = true) @contract, @full = contract, full end # Formats any type of Contract. def contract(contract = @contract) @@ -21,26 +21,26 @@ end # Formats Hash contracts. def hash_contract(hash) @full = true - hash.inject({}) { |repr, (k, v)| + hash.inject({}) do |repr, (k, v)| repr.merge(k => InspectWrapper.new(contract(v), @full)) - }.inspect + end.inspect end # Formats Array contracts. def array_contract(array) @full = true - array.map{ |v| InspectWrapper.new(contract(v), @full) }.inspect + 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) + def initialize(value, full = true) @value, @full = value, full end # Inspect different types of contract values. # Contracts module prefix will be removed from classes. @@ -49,11 +49,11 @@ # 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 has_useful_to_s? + return delim(@value.to_s) if useful_to_s? @value.inspect.gsub(/^Contracts::/, '') end def delim(value) @full ? "(#{value})" : "#{value}" @@ -63,29 +63,34 @@ def to_s inspect end private + def empty_val? - @value.nil? || @value == "" + @value.nil? || @value == '' end def full? @full || - @value.is_a?(Hash) || @value.is_a?(Array) || - (!plain? && has_useful_to_s?) + @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 has_useful_to_s? + def useful_to_s? # Useless to_s value or no custom to_s behavious defined - # Ruby < 2.0 makes inspect call to_s so this won't work - @value.to_s != "" && @value.to_s != @value.inspect + @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) @@ -93,11 +98,11 @@ end def to_a types = [] @types.each_with_index do |type, i| - if i == @types.length-1 + if i == @types.length - 1 # Get the param out of the `param => result` part types << [type.first.first.source, type.first.first] else types << [type.source, type] end @@ -116,12 +121,12 @@ @params = params end def to_a params = [] - @params.each_with_index do |param, i| - #YARD::Parser::Ruby::AstNode + @params.each do |param| + # YARD::Parser::Ruby::AstNode next if param.nil? if param.type == :list param.each do |p| next if p.nil? params << build_param_element(p) @@ -132,10 +137,11 @@ end params end private + def build_param_element(param) type = param.type ident = param.jump(:ident, :label).last.to_sym [type, ident] end @@ -161,19 +167,22 @@ 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])) + result[h[0].jump(:label).last.to_sym] = + Contracts::Formatters::InspectWrapper.new(type(h[1])) end result end # Formats Array type. def array_type(array) # This works because Ast inherits from Array. - array.map{ |v| Contracts::Formatters::InspectWrapper.new(type(v)) }.inspect + array.map do |v| + Contracts::Formatters::InspectWrapper.new(type(v)) + end.inspect end end class ParamContracts def initialize(param_string, types_string) @@ -188,11 +197,11 @@ i = named_count = 0 @params.each do |param| param_type, param = param on_named = param_type == :named_arg || - (named_count > 0 && param_type == :ident) + (named_count > 0 && param_type == :ident) i -= named_count if on_named type, type_ast = @types[i] con = get_contract_value(type) type = TypeAST.new(type_ast).type @@ -238,21 +247,18 @@ desc = "#{desc}".empty? ? '' : "+#{desc}+" [type, desc] end private + # The contract starts as a string, but we need to get it's real value # so that we can call to_s on it. def get_contract_value(type) con = type begin con = Contracts.const_get(type) - rescue Exception #NameError - begin - con = eval(type) - rescue Exception - con - end + rescue StandardError # NameError + con = eval(type) rescue StandardError end con end end end