module RSpec::Matchers::Pretty
@api private Contains logic to facilitate converting ruby symbols and objects to english phrases.
Public Class Methods
split_words(sym)
click to toggle source
@api private Converts a symbol into an english expression.
# File lib/rspec/matchers/pretty.rb, line 9 def split_words(sym) sym.to_s.gsub(/_/, ' ') end
Public Instance Methods
name()
click to toggle source
@api private Provides a name for the matcher.
# File lib/rspec/matchers/pretty.rb, line 45 def name defined?(@name) ? @name : underscore(self.class.name.split("::").last) end
name_to_sentence()
click to toggle source
@private Provides an English expression for the matcher name.
# File lib/rspec/matchers/pretty.rb, line 39 def name_to_sentence split_words(name) end
to_sentence(words)
click to toggle source
@api private Converts a collection of objects into an english expression.
# File lib/rspec/matchers/pretty.rb, line 16 def to_sentence(words) return " #{words.inspect}" if !words || Struct === words words = Array(words).map { |w| to_word(w) } case words.length when 0 "" when 1 " #{words[0]}" when 2 " #{words[0]} and #{words[1]}" else " #{words[0...-1].join(', ')}, and #{words[-1]}" end end
to_word(item)
click to toggle source
@api private Converts the given item to string suitable for use in a list expression.
# File lib/rspec/matchers/pretty.rb, line 33 def to_word(item) is_matcher_with_description?(item) ? item.description : item.inspect end
underscore(camel_cased_word)
click to toggle source
@private Borrowed from ActiveSupport
# File lib/rspec/matchers/pretty.rb, line 51 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr!("-", "_") word.downcase! word end
Private Instance Methods
improve_hash_formatting(inspect_string)
click to toggle source
`{ :a => 5, :b => 2 }.inspect` produces:
{:a=>5, :b=>2}
…but it looks much better as:
{:a => 5, :b => 2}
This is idempotent and safe to run on a string multiple times.
# File lib/rspec/matchers/pretty.rb, line 72 def improve_hash_formatting(inspect_string) inspect_string.gsub(/(\S)=>(\S)/, '\1 => \2') end
is_matcher_with_description?(object)
click to toggle source
# File lib/rspec/matchers/pretty.rb, line 62 def is_matcher_with_description?(object) RSpec::Matchers.is_a_matcher?(object) && object.respond_to?(:description) end
split_words(sym)
click to toggle source
@api private Converts a symbol into an english expression.
# File lib/rspec/matchers/pretty.rb, line 9 def split_words(sym) sym.to_s.gsub(/_/, ' ') end