class RSpec::Scaffold::ConditionExhibit
Attributes
indent[R]
second_indent[R]
Public Class Methods
new(condition, indent = ' ')
click to toggle source
Calls superclass method
# File lib/rspec/scaffold/condition_exhibit.rb, line 7 def initialize(condition, indent = ' ') super(condition) @indent, @second_indent = indent, indent + (' ' * 2) end
Public Instance Methods
call_with_modifiers(method_name, modifiers = [])
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 49 def call_with_modifiers(method_name, modifiers = []) Array(modifiers).inject(public_send(method_name)) { |txt, modifier| public_send(modifier, txt) } end
conditional_parts(condition_part, contexts = [])
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 53 def conditional_parts(condition_part, contexts = []) condition_part = self.class.new(condition_part, indent) contexts.concat(condition_part.context(:statement, [:edit_prefix, :escape]) do |lines| lines.concat condition_part.it(:if_text, :escape) end) if condition_part.should_print? condition_part.else_text contexts.concat(condition_part.context(:statement, [:negate, :edit_prefix, :escape]) do |lines| lines.concat condition_part.it(:else_text, :escape) end) end contexts end
context(*args) { |list| ... }
click to toggle source
Private
# File lib/rspec/scaffold/condition_exhibit.rb, line 37 def context(*args) list = [%Q(#{indent}context #{call_with_modifiers(*args)} do), %Q(#{second_indent}before {})] yield list if block_given? list << %Q(#{indent}end) list end
edit_prefix(txt)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 90 def edit_prefix(txt) txt.sub! /^if /, 'when ' txt.sub! /^not if /, 'unless ' txt.sub! /^if not /, 'unless ' if txt !~ /^(when|unless|not)/ txt = "when #{txt}" end txt end
escape(txt)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 66 def escape(txt) txt.gsub!(/#\{(.*?)\}/m, '\#{\1}') # escape interpolations txt.gsub!(/"/m, "'") # replace double quotes with single quotes %Q("#{truncate(txt)}") end
it(*args)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 44 def it(*args) return [] unless we_should_print? public_send(args.first) [%Q(), %Q(#{second_indent}it #{call_with_modifiers(*args)} do), %Q(#{second_indent}end)] end
negate(txt)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 82 def negate(txt) if txt[/^unless /] txt.sub /^unless /, 'if ' else "not #{txt}" end end
render(contexts = [])
click to toggle source
@param [Array] contexts just used for recursive calls @return [Array]
# File lib/rspec/scaffold/condition_exhibit.rb, line 14 def render(contexts = []) contexts.concat(context(:statement, [:edit_prefix, :escape]) do |lines| lines.concat it(:if_text, :escape) lines << %Q() if nested_conditions.any? nested_conditions.each { |nc| self.class.new(nc, second_indent).render(lines) } lines end) contexts << %Q() if parts.empty? contexts.concat(context(:statement, [:negate, :edit_prefix, :escape]) do |lines| lines.concat it(:else_text, :escape) end) end parts.each do |condition_part| contexts.concat conditional_parts(condition_part) end contexts end
truncate(txt, length = 120)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 72 def truncate(txt, length = 120) txt = txt.lstrip length -= 3 if txt.length > length txt[0, length] + '...' else txt end end
we_should_print?(txt)
click to toggle source
# File lib/rspec/scaffold/condition_exhibit.rb, line 100 def we_should_print?(txt) !txt.empty? and txt !~ /\n/ end
Also aliased as: should_print?