Class: RSpec::Scaffold::ConditionExhibit
- Inherits:
-
Ryan::Condition
- Object
- Ryan::Condition
- RSpec::Scaffold::ConditionExhibit
- Defined in:
- lib/rspec/scaffold/condition_exhibit.rb
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#second_indent ⇒ Object
readonly
Returns the value of attribute second_indent.
Instance Method Summary collapse
- #call_with_modifiers(method_name, modifiers = []) ⇒ Object
- #conditional_parts(condition_part, contexts = []) ⇒ Object
-
#context(*args) {|list| ... } ⇒ Object
Private.
- #edit_prefix(txt) ⇒ Object
- #escape(txt) ⇒ Object
-
#initialize(condition, indent = ' ') ⇒ ConditionExhibit
constructor
A new instance of ConditionExhibit.
- #it(*args) ⇒ Object
- #negate(txt) ⇒ Object
- #render(contexts = []) ⇒ Array
- #truncate(txt, length = 120) ⇒ Object
- #we_should_print?(txt) ⇒ Boolean (also: #should_print?)
Constructor Details
#initialize(condition, indent = ' ') ⇒ ConditionExhibit
Returns a new instance of ConditionExhibit.
7 8 9 10 |
# File 'lib/rspec/scaffold/condition_exhibit.rb', line 7 def initialize(condition, indent = ' ') super(condition) @indent, @second_indent = indent, indent + (' ' * 2) end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Returns the value of attribute indent.
5 6 7 |
# File 'lib/rspec/scaffold/condition_exhibit.rb', line 5 def indent @indent end |
#second_indent ⇒ Object (readonly)
Returns the value of attribute second_indent.
5 6 7 |
# File 'lib/rspec/scaffold/condition_exhibit.rb', line 5 def second_indent @second_indent end |
Instance Method Details
#call_with_modifiers(method_name, modifiers = []) ⇒ Object
49 50 51 |
# 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 = []) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# 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| ... } ⇒ Object
Private
37 38 39 40 41 42 |
# 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) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# 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) ⇒ Object
66 67 68 69 70 |
# 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) ⇒ Object
44 45 46 47 |
# 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) ⇒ Object
82 83 84 85 86 87 88 |
# 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 = []) ⇒ Array
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Boolean Also known as: should_print?
100 101 102 |
# File 'lib/rspec/scaffold/condition_exhibit.rb', line 100 def we_should_print?(txt) !txt.empty? and txt !~ /\n/ end |