Module: SuperTemplate::Base::ClassMethods

Included in:
SuperTemplate::Base
Defined in:
lib/super_template/base.rb

Instance Method Summary collapse

Instance Method Details

#compile(raise_errors: false, force: false) ⇒ Object



43
44
45
# File 'lib/super_template/base.rb', line 43

def compile(raise_errors: false, force: false)
  @__st_compiled ||= compiler.compile(raise_errors: raise_errors, force: force)
end

#compiled? ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/super_template/base.rb', line 39

def compiled?
  @__st_compiled ||= false
end

#compiler ⇒ Object



47
48
49
# File 'lib/super_template/base.rb', line 47

def compiler
  @__st_compiler ||= SuperTemplate::Compiler.new(self)
end

#inline_template ⇒ Object



63
64
65
# File 'lib/super_template/base.rb', line 63

def inline_template
  @__st_inline_template
end

#sidecar_files(extensions) ⇒ Object

Find sidecar files for the given extensions.

The provided array of extensions is expected to contain strings starting without the dot, example: ["erb", "haml"].

For example, one might collect sidecar CSS files that need to be compiled.

Parameters:

  • extensions (Array<String>) —

    Extensions of which to return matching sidecar files.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/super_template/base.rb', line 74

def sidecar_files(extensions)
  return [] unless source_location

  extensions = extensions.join(",")

  # view files in a directory named like the component
  directory = File.dirname(source_location)
  filename = File.basename(source_location, ".rb")
  component_name = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(name))

  # Add support for nested components defined in the same file.
  #
  # for example
  #
  # class MyTemplate < SuperTemplate::Base
  #   class MyTemplate < SuperTemplate::Base
  #   end
  # end
  #
  # Without this, `MyOtherComponent` will not look for `my_component/my_other_component.html.erb`
  nested_component_files =
    if name.include?("::") && component_name != filename
      Dir["#{directory}/#{filename}/#{component_name}.*{#{extensions}}"]
    else
      []
    end

  # view files in the same directory as the component
  sidecar_files = Dir["#{directory}/#{component_name}.*{#{extensions}}"]

  sidecar_directory_files = Dir["#{directory}/#{component_name}/#{filename}.*{#{extensions}}"]

  (sidecar_files - [source_location] + sidecar_directory_files + nested_component_files).uniq
end

#source_location ⇒ Object



35
36
37
# File 'lib/super_template/base.rb', line 35

def source_location
  @source_location ||= Object.const_source_location(self.name)[0]
end

#strip_trailing_whitespace(value = true) ⇒ Object

Strips trailing whitespace from templates before compiling them.

class MyTemplate < SuperTemplate::Base
  strip_trailing_whitespace
end

Parameters:

  • value (Boolean) (defaults to: true) —

    Whether to strip newlines.



24
25
26
# File 'lib/super_template/base.rb', line 24

def strip_trailing_whitespace(value = true)
  @__st_strip_trailing_whitespace = value
end

#strip_trailing_whitespace? ⇒ Boolean

Whether trailing whitespace will be stripped before compilation.

Returns:

  • (Boolean)


31
32
33
# File 'lib/super_template/base.rb', line 31

def strip_trailing_whitespace?
  @__st_strip_trailing_whitespace
end

#template(extension, source) ⇒ Object

Parameters:

  • extension (Symbol) —

    extension

  • source (String) —

    template source



53
54
55
56
57
58
59
60
61
# File 'lib/super_template/base.rb', line 53

def template(extension, source)
  caller = caller_locations(1..1)[0]
  @__st_inline_template = OpenStruct.new(
    source: source,
    type: extension,
    path: caller.absolute_path || caller.path,
    lineno: caller.lineno
  )
end