Class: SeeingIsBelieving::WrapExpressions

Inherits:
Object
  • Object
show all
Defined in:
lib/seeing_is_believing/wrap_expressions.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program, wrappings) ⇒ WrapExpressions

Returns a new instance of WrapExpressions.



12
13
14
15
16
17
18
19
20
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 12

def initialize(program, wrappings)
  self.before_all  = wrappings.fetch :before_all,  -> { '' }
  self.after_all   = wrappings.fetch :after_all,   -> { '' }
  self.before_each = wrappings.fetch :before_each, -> * { '' }
  self.after_each  = wrappings.fetch :after_each,  -> * { '' }
  self.wrappings   = {}
  self.code        = Code.new(program, 'program-without-annotations')
  code.syntax.valid? || raise(::SyntaxError, code.syntax.error_message)
end

Class Method Details

.call(program, wrappings) ⇒ Object



8
9
10
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 8

def self.call(program, wrappings)
  new(program, wrappings).call
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 22

def call
  @called ||= begin
    wrap_recursive code.root

    wrappings = wrappings().sort_by(&:first)

    wrappings.each do |line_num, (range, last_col, meta)|
      case meta
      when :total_fucking_failure
        rewriter.replace range,  '.....TOTAL FUCKING FAILURE!.....'
      when :match_current_line
        rewriter.insert_before range, '~' # Regexp#~
      end
    end

    wrappings.each do |line_num, (range, last_col, meta)|
      rewriter.insert_before range, before_each.call(line_num)
    end

    wrappings.each do |line_num, (range, last_col, meta)|
      rewriter.insert_after range, after_each.call(line_num)
    end

    rewriter.insert_before root_range, before_all.call
    rewriter.insert_after  root_range, after_all_text
    rewriter.process
  end
end