Class: TextTube::Base
Overview
Inherit from this and wrap it around a string and you can then run filters against it.
Instance Attribute Summary collapse
-
#options ⇒ Hash
This instance’s options.
Class Method Summary collapse
-
.filters ⇒ Object
Filters added.
-
.inherited(subclass) ⇒ Object
A clean slate.
-
.options ⇒ Object
Global options.
-
.register(filter = nil, &block) ⇒ Object
Register a module of filters.
-
.reset! ⇒ Object
remove all filters.
Instance Method Summary collapse
-
#filter(*order_and_options) ⇒ Object
Run the filters.
-
#filters ⇒ Object
See all added filters.
-
#initialize(text, options = {}) ⇒ Base
constructor
A new instance of Base.
Methods included from CoreExtensions
Constructor Details
#initialize(text, options = {}) ⇒ Base
Returns a new instance of Base.
52 53 54 55 56 |
# File 'lib/texttube/base.rb', line 52 def initialize( text, ={} ) @options = @filters ||= [] super text end |
Instance Attribute Details
#options ⇒ Hash
This instance’s options.
46 47 48 |
# File 'lib/texttube/base.rb', line 46 def @options end |
Class Method Details
.filters ⇒ Object
Filters added.
74 75 76 |
# File 'lib/texttube/base.rb', line 74 def filters @filters ||= [] end |
.inherited(subclass) ⇒ Object
A clean slate
113 114 115 |
# File 'lib/texttube/base.rb', line 113 def inherited(subclass) subclass.reset! end |
.options ⇒ Object
Global options. Every descendant will get these.
62 63 64 |
# File 'lib/texttube/base.rb', line 62 def @options ||= {} end |
.register(filter = nil, &block) ⇒ Object
Register a module of filters. I could’ve made it so there’s no need to write the ‘filter_with` part, but this way your code will be clearer to those reading it later, and you can add in any other stuff you want done too, you don’t just have to set up a filter.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/texttube/base.rb', line 96 def register( filter=nil, &block ) @filters ||= [] if block name = filter filter = Module.new do extend Filterable end filter.instance_eval &block end @filters += filter.filters include filter end |
.reset! ⇒ Object
remove methods too.
remove all filters
68 69 70 |
# File 'lib/texttube/base.rb', line 68 def reset! @filters = [] end |
Instance Method Details
#filter ⇒ Object #filter(filters) ⇒ Object #filter(filters_and_options) ⇒ Object
Run the filters. If the names of filters are provided as arguments then only those filters will be run, in that order (left first, to right).
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/texttube/base.rb', line 139 def filter( * ) if .last.respond_to?(:keys) *order, = * else order, = , {} end order = order.flatten order = @options.fetch :order, self.class.filters if order.empty? order.inject(self){|current,filter| send filter, current, } end |
#filters ⇒ Object
See all added filters.
153 154 155 |
# File 'lib/texttube/base.rb', line 153 def filters self.class.filters end |