Module: Flutterby::Filters

Extended by:
Filters
Included in:
Filters
Defined in:
lib/flutterby/filters.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(fmts, &blk) ⇒ Object



29
30
31
32
33
# File 'lib/flutterby/filters.rb', line 29

def add(fmts, &blk)
  Array(fmts).each do |fmt|
    define_singleton_method("process_#{fmt}!", &blk)
  end
end

.apply!(input, filters, view:, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flutterby/filters.rb', line 13

def apply!(input, filters, view:, &blk)
  # Apply all filters
  filters.inject(input) do |body, filter|
    meth = "process_#{filter}!"

    if Filters.respond_to?(meth)
      Filters.send(meth, body, view: view, &blk)
    elsif template = tilt(filter, body)
      template.render(view, view.locals, &blk).html_safe
    else
      Flutterby.logger.warn "Unsupported filter '#{filter}'"
      body
    end
  end
end

.tilt(format, body, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/flutterby/filters.rb', line 35

def tilt(format, body, options = {})
  default_options = {
    "erb" => { engine_class: Erubis::Auto::EscapedEruby }
  }

  options = default_options.fetch(format, {}).merge(options)

  t = Tilt[format] and t.new(options) { body }
end

Instance Method Details

#add(fmts, &blk) ⇒ Object



29
30
31
32
33
# File 'lib/flutterby/filters.rb', line 29

def add(fmts, &blk)
  Array(fmts).each do |fmt|
    define_singleton_method("process_#{fmt}!", &blk)
  end
end

#apply!(input, filters, view:, &blk) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flutterby/filters.rb', line 13

def apply!(input, filters, view:, &blk)
  # Apply all filters
  filters.inject(input) do |body, filter|
    meth = "process_#{filter}!"

    if Filters.respond_to?(meth)
      Filters.send(meth, body, view: view, &blk)
    elsif template = tilt(filter, body)
      template.render(view, view.locals, &blk).html_safe
    else
      Flutterby.logger.warn "Unsupported filter '#{filter}'"
      body
    end
  end
end

#tilt(format, body, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/flutterby/filters.rb', line 35

def tilt(format, body, options = {})
  default_options = {
    "erb" => { engine_class: Erubis::Auto::EscapedEruby }
  }

  options = default_options.fetch(format, {}).merge(options)

  t = Tilt[format] and t.new(options) { body }
end