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
- .polluted_method(name) ⇒ Object
- .polluted_methods_list ⇒ Object
- .purge_class_pollution ⇒ 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.
94 95 96 97 98 99 |
# File 'lib/serbea/pipeline.rb', line 94 def initialize(binding, value) self.class.purge_class_pollution @binding = binding @context = binding.receiver @value = value end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
138 139 140 |
# File 'lib/serbea/pipeline.rb', line 138 def method_missing(...) filter(...) end |
Class Method Details
.autoescape ⇒ Object
57 58 59 |
# File 'lib/serbea/pipeline.rb', line 57 def self.autoescape @autoescape.nil? ? true : @autoescape end |
.autoescape=(config_boolean) ⇒ Object
54 55 56 |
# File 'lib/serbea/pipeline.rb', line 54 def self.autoescape=(config_boolean) @autoescape = config_boolean end |
.deny_value_method(name) ⇒ Object
68 69 70 |
# File 'lib/serbea/pipeline.rb', line 68 def self.deny_value_method(name) value_methods_denylist.merge Array(name) end |
.exec(template, locals = {}, include_helpers: nil, **kwargs) ⇒ Object
Exec the pipes!
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/serbea/pipeline.rb', line 21 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.empty? ? kwargs : locals) pipeline_obj.output end |
.output_processor ⇒ Proc
48 49 50 51 52 |
# File 'lib/serbea/pipeline.rb', line 48 def self.output_processor @output_processor ||= lambda do |input| (!input.html_safe? && self.autoescape) ? Erubi.h(input) : input.html_safe end end |
.output_processor=(processor) ⇒ Object
43 44 45 |
# File 'lib/serbea/pipeline.rb', line 43 def self.output_processor=(processor) @output_processor = processor end |
.polluted_method(name) ⇒ Object
87 88 89 |
# File 'lib/serbea/pipeline.rb', line 87 def self.polluted_method(name) polluted_methods_list.merge Array(name) end |
.polluted_methods_list ⇒ Object
90 91 92 |
# File 'lib/serbea/pipeline.rb', line 90 def self.polluted_methods_list @polluted_methods_list ||= Set.new(%i(select to_json)) end |
.purge_class_pollution ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/serbea/pipeline.rb', line 75 def self.purge_class_pollution @pollution_purged ||= begin polluted_methods_list.each do |name| define_method name do |*args, **kwargs| filter(name, *args, **kwargs) end end true end end |
.raise_on_missing_filters ⇒ Object
64 65 66 |
# File 'lib/serbea/pipeline.rb', line 64 def self.raise_on_missing_filters @raise_on_missing_filters ||= false end |
.raise_on_missing_filters=(config_boolean) ⇒ Object
61 62 63 |
# File 'lib/serbea/pipeline.rb', line 61 def self.raise_on_missing_filters=(config_boolean) @raise_on_missing_filters = config_boolean end |
.value_methods_denylist ⇒ Object
71 72 73 |
# File 'lib/serbea/pipeline.rb', line 71 def self.value_methods_denylist @value_methods_denylist ||= Set.new end |
Instance Method Details
#filter(name, *args, **kwargs) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/serbea/pipeline.rb', line 101 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
130 131 132 |
# File 'lib/serbea/pipeline.rb', line 130 def to_s self.class.output_processor.call(@value.is_a?(String) ? @value : @value.to_s) end |
#value(callback = nil) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/serbea/pipeline.rb', line 142 def value(callback = nil) return @value unless callback @value = if callback.is_a?(Proc) callback.(@value) else callback end self end |
#| ⇒ Object
134 135 136 |
# File 'lib/serbea/pipeline.rb', line 134 def |(*) self end |