Class: SeeingIsBelieving::WrapExpressions
- Inherits:
-
Object
- Object
- SeeingIsBelieving::WrapExpressions
- Includes:
- ParserHelpers
- Defined in:
- lib/seeing_is_believing/wrap_expressions.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(program, wrappings) ⇒ WrapExpressions
constructor
A new instance of WrapExpressions.
Methods included from ParserHelpers
#comments_from, #heredoc?, #heredoc_hack, #initialize_parser, #void_value?
Constructor Details
#initialize(program, wrappings) ⇒ WrapExpressions
Returns a new instance of WrapExpressions.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 42 def initialize(program, wrappings) self.program = program self.before_all = wrappings.fetch :before_all, ''.freeze self.after_all = wrappings.fetch :after_all, ''.freeze self.before_each = wrappings.fetch :before_each, -> * { '' } self.after_each = wrappings.fetch :after_each, -> * { '' } self.buffer, parser, self.rewriter = initialize_parser(program, 'program-without-annotations') self.root = parser.parse buffer self.wrappings = {} rescue Parser::SyntaxError => e raise ::SyntaxError, e. end |
Class Method Details
.call(program, wrappings) ⇒ Object
38 39 40 |
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 38 def self.call(program, wrappings) new(program, wrappings).call end |
Instance Method Details
#call ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/seeing_is_believing/wrap_expressions.rb', line 55 def call @called ||= begin find_wrappings if root # file may be empty rewriter.insert_before root.location.expression, before_all wrappings.each do |line_num, (range, last_col, )| rewriter.insert_before range, before_each.call(line_num) if == :wrap_in_braces rewriter.insert_before range, '{' rewriter.insert_after range, '}' elsif == :total_fucking_failure rewriter.replace range, '.....TOTAL FUCKING FAILURE!.....' end rewriter.insert_after range, after_each.call(line_num) end # another stupid hack to get around a heredoc at the end of the document # hopefully we can remove this after next version of Parser is released last_index, (range, col) = wrappings.max_by(&:first) range ||= root.location.expression rewriter.insert_after range, after_all end rewriter.process end end |