Class: Serbea::Pipeline
- Inherits:
-
Object
- Object
- Serbea::Pipeline
- Defined in:
- lib/serbea/pipeline.rb
Defined Under Namespace
Modules: Helper
Class Method Summary collapse
- .autoescape ⇒ Object
- .autoescape=(config_boolean) ⇒ Object
- .deny_value_method(name) ⇒ Object
-
.exec(template, locals = {}, include_helpers: nil, **kwargs) ⇒ Object
Exec the pipes!.
- .output_processor ⇒ Proc
- .output_processor=(processor) ⇒ Object
- .raise_on_missing_filters ⇒ Object
- .raise_on_missing_filters=(config_boolean) ⇒ Object
- .value_methods_denylist ⇒ Object
Instance Method Summary collapse
- #filter(name, *args, **kwargs) ⇒ Object
-
#initialize(binding, value) ⇒ Pipeline
constructor
A new instance of Pipeline.
- #method_missing ⇒ Object
- #to_s ⇒ Object
- #value(callback = nil) ⇒ Object
- #| ⇒ Object
Constructor Details
#initialize(binding, value) ⇒ Pipeline
Returns a new instance of Pipeline.
77 78 79 80 81 |
# File 'lib/serbea/pipeline.rb', line 77 def initialize(binding, value) @binding = binding @context = binding.receiver @value = value end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
120 121 122 |
# File 'lib/serbea/pipeline.rb', line 120 def method_missing(...) filter(...) end |
Class Method Details
.autoescape ⇒ Object
59 60 61 |
# File 'lib/serbea/pipeline.rb', line 59 def self.autoescape @autoescape.nil? ? true : @autoescape end |
.autoescape=(config_boolean) ⇒ Object
56 57 58 |
# File 'lib/serbea/pipeline.rb', line 56 def self.autoescape=(config_boolean) @autoescape = config_boolean end |
.deny_value_method(name) ⇒ Object
70 71 72 |
# File 'lib/serbea/pipeline.rb', line 70 def self.deny_value_method(name) value_methods_denylist.merge Array(name) end |
.exec(template, locals = {}, include_helpers: nil, **kwargs) ⇒ Object
Exec the pipes!
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/serbea/pipeline.rb', line 23 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_processor ⇒ Proc
50 51 52 53 54 |
# File 'lib/serbea/pipeline.rb', line 50 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
45 46 47 |
# File 'lib/serbea/pipeline.rb', line 45 def self.output_processor=(processor) @output_processor = processor end |
.raise_on_missing_filters ⇒ Object
66 67 68 |
# File 'lib/serbea/pipeline.rb', line 66 def self.raise_on_missing_filters @raise_on_missing_filters ||= false end |
.raise_on_missing_filters=(config_boolean) ⇒ Object
63 64 65 |
# File 'lib/serbea/pipeline.rb', line 63 def self.raise_on_missing_filters=(config_boolean) @raise_on_missing_filters = config_boolean end |
.value_methods_denylist ⇒ Object
73 74 75 |
# File 'lib/serbea/pipeline.rb', line 73 def self.value_methods_denylist @value_methods_denylist ||= Set.new end |
Instance Method Details
#filter(name, *args, **kwargs) ⇒ Object
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 |
# File 'lib/serbea/pipeline.rb', line 83 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 @value = @value.send(name, *real_args, **kwargs, &block) else @value = @value.send(name, *args, **kwargs) end elsif @context.respond_to?(name) @value = @context.send(name, @value, *args, **kwargs) elsif @binding.local_variables.include?(name) var = @binding.local_variable_get(name) if var.respond_to?(:call) @value = var.call(@value, *args, **kwargs) else "Serbea warning: Filter '#{name}' does not respond to call".tap do |warning| self.class.raise_on_missing_filters ? raise(Serbea::FilterMissing, warning) : STDERR.puts(warning) end end else "Serbea warning: Filter `#{name}' not found".tap do |warning| self.class.raise_on_missing_filters ? raise(Serbea::FilterMissing, warning) : STDERR.puts(warning) end end self end |
#to_s ⇒ Object
112 113 114 |
# File 'lib/serbea/pipeline.rb', line 112 def to_s self.class.output_processor.call(@value.is_a?(String) ? @value : @value.to_s) end |
#value(callback = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/serbea/pipeline.rb', line 124 def value(callback = nil) return @value unless callback @value = if callback.is_a?(Proc) callback.(@value) else callback end self end |
#| ⇒ Object
116 117 118 |
# File 'lib/serbea/pipeline.rb', line 116 def |(*) self end |