Class: ORB::RailsTemplate
- Inherits:
-
Object
- Object
- ORB::RailsTemplate
- Defined in:
- lib/orb/rails_template.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(template, source = nil) ⇒ Object
- #find_offset(snippet, src_tokens, snippet_error_column) ⇒ Object
- #offset_from_token(token) ⇒ Object
- #supports_streaming? ⇒ Boolean
- #translate_location(spot, backtrace_location, source) ⇒ Object
Class Method Details
.options ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/orb/rails_template.rb', line 8 def ||= { generator: ::Temple::Generators::RailsOutputBuffer, use_html_safe: true, streaming: true, buffer_class: 'ActionView::OutputBuffer', disable_capture: true, } end |
.set_options(opts) ⇒ Object
18 19 20 |
# File 'lib/orb/rails_template.rb', line 18 def (opts) .update(opts) end |
Instance Method Details
#call(template, source = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/orb/rails_template.rb', line 23 def call(template, source = nil) source ||= template.source = RailsTemplate. # Make the filename available in parser etc. = .merge(file: template.identifier) if template.respond_to?(:identifier) # Set type = .merge(format: :xhtml) if template.respond_to?(:type) && template.type == 'text/xml' # Annotations if ActionView::Base.try(:annotate_rendered_view_with_filenames) && template.format == :html = .merge( preamble: "<!-- BEGIN #{template.short_identifier} -->\n", postamble: "<!-- END #{template.short_identifier} -->\n", ) end # Pipe through the ORB Temple engine ORB::Temple::Engine.new().call(source) end |
#find_offset(snippet, src_tokens, snippet_error_column) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/orb/rails_template.rb', line 65 def find_offset(snippet, src_tokens, snippet_error_column) offset = 0 passed_tokens = [] # Pass over tokens until we are just over the snippet error column # then the column of the last token is the offset (without the token static offset for tags {% %}) while (tok = src_tokens.shift) offset = snippet.index(tok.value, offset) raise "text not found" unless offset raise "we went too far" if offset > snippet_error_column passed_tokens << tok end rescue StandardError offset_token = passed_tokens.last offset_from_token(offset_token) end |
#offset_from_token(token) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/orb/rails_template.rb', line 83 def offset_from_token(token) case token.type when :tag_open, :tag_close token.column + 1 when :public_comment, :private_comment token.column + 4 when :block_open, :block_close token.column + 2 + token.value.length when :printing_expression, :control_expression token.column + 2 end end |
#supports_streaming? ⇒ Boolean
96 97 98 |
# File 'lib/orb/rails_template.rb', line 96 def supports_streaming? RailsTemplate.[:streaming] end |
#translate_location(spot, backtrace_location, source) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/orb/rails_template.rb', line 46 def translate_location(spot, backtrace_location, source) offending_line_source = source.lines[backtrace_location.lineno - 1] tokens = ORB::Utils::ORB.tokenize(offending_line_source) new_column = find_offset(spot[:snippet], tokens, spot[:first_column]) lineno_delta = spot[:first_lineno] - backtrace_location.lineno spot[:first_lineno] -= lineno_delta spot[:last_lineno] -= lineno_delta column_delta = spot[:first_column] - new_column spot[:first_column] -= column_delta spot[:last_column] -= column_delta spot[:script_lines] = source.lines spot rescue StandardError => _e spot end |