Class: ORB::RailsTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/orb/rails_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionsObject



8
9
10
11
12
13
14
15
16
# File 'lib/orb/rails_template.rb', line 8

def options
  @options ||= {
    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 set_options(opts)
  options.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
  options = RailsTemplate.options

  # Make the filename available in parser etc.
  options = options.merge(file: template.identifier) if template.respond_to?(:identifier)

  # Set type
  options = options.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
    options = options.merge(
      preamble: "<!-- BEGIN #{template.short_identifier} -->\n",
      postamble: "<!-- END #{template.short_identifier} -->\n",
    )
  end

  # Pipe through the ORB Temple engine
  ORB::Temple::Engine.new(options).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

Returns:

  • (Boolean)


96
97
98
# File 'lib/orb/rails_template.rb', line 96

def supports_streaming?
  RailsTemplate.options[: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