Module: Tapioca::Helpers::Test::Template

Extended by:
T::Helpers, T::Sig
Included in:
DslCompiler
Defined in:
lib/tapioca/helpers/test/template.rb

Constant Summary collapse

ERB_SUPPORTS_KVARGS =
T.let(
  ::ERB.instance_method(:initialize).parameters.assoc(:key), T.nilable([Symbol, Symbol])
)

Instance Method Summary collapse

Instance Method Details

#indented(str, indent) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/tapioca/helpers/test/template.rb', line 39

def indented(str, indent)
  str.lines.map! do |line|
    next line if line.chomp.empty?

    (" " * indent) + line
  end.join
end

#rails_version(selector) ⇒ Object



23
24
25
# File 'lib/tapioca/helpers/test/template.rb', line 23

def rails_version(selector)
  ::Gem::Requirement.new(selector).satisfied_by?(ActiveSupport.gem_version)
end

#ruby_version(selector) ⇒ Object



18
19
20
# File 'lib/tapioca/helpers/test/template.rb', line 18

def ruby_version(selector)
  ::Gem::Requirement.new(selector).satisfied_by?(::Gem::Version.new(RUBY_VERSION))
end

#template(src, trim_mode: ">") ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/tapioca/helpers/test/template.rb', line 28

def template(src, trim_mode: ">")
  erb = if ERB_SUPPORTS_KVARGS
    ::ERB.new(src, trim_mode: trim_mode)
  else
    ::ERB.new(src, nil, trim_mode)
  end

  erb.result(binding)
end