Class: Hpreserve::Filters
- Inherits:
-
Object
- Object
- Hpreserve::Filters
- Defined in:
- lib/hpreserve/filters.rb
Overview
Acts as a sandbox to run filters in, most everything except the self.parse method was stolen from Liquid’s Strainer class. Almost all the standard class methods (especially instance_eval) are undefined, and respond_to? is modified to only return true on methods defined by the filter modules registered.
Constant Summary collapse
- @@filter_modules =
[]
- @@required_methods =
%w(__send__ __id__ debugger run inspect methods respond_to? extend)
Class Method Summary collapse
- .create ⇒ Object
-
.parse(str = '') ⇒ Object
The filter parser expects a string formatted similar to an html element’s “style” attribute:.
- .register(filterset) ⇒ Object
Instance Method Summary collapse
-
#inspect ⇒ Object
:nodoc keeping inspect around simply to make irb happy.
- #respond_to?(method) ⇒ Boolean
- #run(filter, node, *args) ⇒ Object
Class Method Details
.create ⇒ Object
20 21 22 23 24 |
# File 'lib/hpreserve/filters.rb', line 20 def self.create filterset = Filters.new @@filter_modules.each { |m| filterset.extend m } filterset end |
.parse(str = '') ⇒ Object
The filter parser expects a string formatted similar to an html element’s “style” attribute:
* <tt>filter="date: %d %b; truncate_words: 15, ...; capitalize"</tt>
(to provide an example of two currently filters you wouldn’t use together…) (todo: better examples fool!). Filters are separated by semicolons and are executed in order given. If the filter needs arguments, those are given after a colon and separated by commas.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hpreserve/filters.rb', line 35 def self.parse(str='') str.split(';').inject([]) do |memo, rule| list = rule.split(':') filter = list.shift.strip set = [filter] unless list.empty? list = list.join(':') # get us back to our original string list = list.split(',') # becase we care about something else set += list.collect {|a| a.strip } # get rid of any pesky whitespace end memo << set end end |
.register(filterset) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/hpreserve/filters.rb', line 13 def self.register(filterset) [filterset].flatten.each do |mod| raise StandardError("passed filter is not a module") unless mod.is_a?(Module) @@filter_modules << mod end end |
Instance Method Details
#inspect ⇒ Object
:nodoc keeping inspect around simply to make irb happy.
53 |
# File 'lib/hpreserve/filters.rb', line 53 def inspect; end |
#respond_to?(method) ⇒ Boolean
55 56 57 58 59 60 |
# File 'lib/hpreserve/filters.rb', line 55 def respond_to?(method) method_name = method.to_s return false if method_name =~ /^__/ return false if @@required_methods.include?(method_name) super end |
#run(filter, node, *args) ⇒ Object
62 63 64 65 |
# File 'lib/hpreserve/filters.rb', line 62 def run(filter, node, *args) __send__(filter, node, *args) if respond_to?(filter) end |