Class: Serbea::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/serbea/pipeline.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binding, value) ⇒ Pipeline

Returns a new instance of Pipeline.



65
66
67
68
69
# File 'lib/serbea/pipeline.rb', line 65

def initialize(binding, value)
  @binding = binding
  @context = binding.receiver
  @value = value
end

Class Method Details

.autoescapeObject



47
48
49
# File 'lib/serbea/pipeline.rb', line 47

def self.autoescape
  @autoescape.nil? ? true : @autoescape
end

.autoescape=(config_boolean) ⇒ Object



44
45
46
# File 'lib/serbea/pipeline.rb', line 44

def self.autoescape=(config_boolean)
  @autoescape = config_boolean
end

.deny_value_method(name) ⇒ Object



58
59
60
# File 'lib/serbea/pipeline.rb', line 58

def self.deny_value_method(name)
  value_methods_denylist.merge Array(name)
end

.exec(template, locals = {}, include_helpers: nil, **kwargs) ⇒ Object

Exec the pipes!

Parameters:

  • template (String)
  • locals (Hash) (defaults to: {})
  • include_helpers (Module) (defaults to: nil)
  • kwargs (Hash)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/serbea/pipeline.rb', line 11

def self.exec(template, locals = {}, include_helpers: nil, **kwargs)
  anon = Class.new do
    include Serbea::Helpers

    attr_accessor :output
  end

  if include_helpers
    anon.include include_helpers
  end

  pipeline_obj = anon.new

  full_template = "{{ #{template} | assign_to: :output }}"

  tmpl = Tilt::SerbeaTemplate.new { full_template }
  tmpl.render(pipeline_obj, locals.presence || kwargs)

  pipeline_obj.output
end

.output_processorProc

Returns:

  • (Proc)


38
39
40
41
42
# File 'lib/serbea/pipeline.rb', line 38

def self.output_processor
  @output_processor ||= lambda do |input|
    (!input.html_safe? && self.autoescape) ? ERB::Util.h(input) : input.html_safe
  end
end

.output_processor=(processor) ⇒ Object

Parameters:

  • processor (Proc)


33
34
35
# File 'lib/serbea/pipeline.rb', line 33

def self.output_processor=(processor)
  @output_processor = processor
end

.raise_on_missing_filtersObject



54
55
56
# File 'lib/serbea/pipeline.rb', line 54

def self.raise_on_missing_filters
  @raise_on_missing_filters ||= false
end

.raise_on_missing_filters=(config_boolean) ⇒ Object



51
52
53
# File 'lib/serbea/pipeline.rb', line 51

def self.raise_on_missing_filters=(config_boolean)
  @raise_on_missing_filters = config_boolean
end

.value_methods_denylistObject



61
62
63
# File 'lib/serbea/pipeline.rb', line 61

def self.value_methods_denylist
  @value_methods_denylist ||= Set.new
end

Instance Method Details

#filter(name, *args, **kwargs) ⇒ Object

TODO: clean this up somehow and still support Ruby 2.5..3.0!



72
73
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
108
109
110
111
112
113
114
115
# File 'lib/serbea/pipeline.rb', line 72

def filter(name, *args, **kwargs)
  if @value.respond_to?(name) && !self.class.value_methods_denylist.include?(name)
    if args.last.is_a?(Proc)
      real_args = args.take(args.length - 1)
      block = args.last
      unless kwargs.empty?
        @value = @value.send(name, *real_args, **kwargs, &block)
      else
        @value = @value.send(name, *real_args, &block)
      end
    else
      unless kwargs.empty?
        @value = @value.send(name, *args, **kwargs)
      else
        @value = @value.send(name, *args)
      end
    end
  elsif @context.respond_to?(name)
    unless kwargs.empty?
      @value = @context.send(name, @value, *args, **kwargs)
    else
      @value = @context.send(name, @value, *args)
    end
  elsif @binding.local_variables.include?(name)
    var = @binding.local_variable_get(name)
    if var.respond_to?(:call)
      unless kwargs.empty?
        @value = var.call(@value, *args, **kwargs)
      else
        @value = var.call(@value, *args)
      end
    else
      "Serbea warning: Filter #{name} does not respond to call".tap do |warning|
        self.class.raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
      end
    end
  else
    "Serbea warning: Filter not found: #{name}".tap do |warning|
      self.class.raise_on_missing_filters ? raise(warning) : STDERR.puts(warning)
    end
  end

  self
end

#to_sObject



117
118
119
# File 'lib/serbea/pipeline.rb', line 117

def to_s
  self.class.output_processor.call(@value.is_a?(String) ? @value : @value.to_s)
end