Class: TextTube::Base

Inherits:
String
  • Object
show all
Defined in:
lib/texttube/base.rb

Overview

Inherit from this and wrap it around a string and you can then run filters against it.

Examples:

module AFilter
  extend TextTube::Filterable

  filter_with :double do |text|
    text * 2
  end

  filter_with :triple do |text|
    text * 3
  end
end

module BFil
  extend TextTube::Filterable

  filter_with :spacial do |current,options|
    current.split(//).join " " 
  end
end

class NeuS < TextTube::Base
  register BFil
  register AFilter
  register do
    filter_with :dashes do |text|
      "---#{text}---"
    end
  end
end

n = NeuS.new "abc"
n.filter
# => "---a b ca b ca b ca b ca b ca b c---"
n
# => "abc"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CoreExtensions

#blank?, #to_constant

Constructor Details

#initialize(text, options = {}) ⇒ Base

Returns a new instance of Base.

Examples:

n = NeuS.new "abc"

Parameters:

  • text (#to_s)

    The original text.

  • options (Hash) (defaults to: {})

    Options that will be passed on to every instance.



52
53
54
55
56
# File 'lib/texttube/base.rb', line 52

def initialize( text, options={} )
  @options = options
  @filters ||= []
  super text
end

Instance Attribute Details

#optionsHash

This instance’s options.

Returns:



46
47
48
# File 'lib/texttube/base.rb', line 46

def options
  @options
end

Class Method Details

.filtersObject

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

.optionsObject

Global options. Every descendant will get these.



62
63
64
# File 'lib/texttube/base.rb', line 62

def options
  @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.

Examples:

class MyFilter < TextTube::Base
  register TextTube::LinkReffing
  register TextTube::Coderay
  register do
    filter_with :dashes do |text|
      "---#{text}---"
    end
  end
end


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

TODO:

remove methods too.

remove all filters



68
69
70
# File 'lib/texttube/base.rb', line 68

def reset!
  @filters = []
end

Instance Method Details

#filterObject #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).

Overloads:

  • #filterObject

    Run all the filters in the order they were registered.

    n = NeuS.new "abc"
    n.filter
    # => "---a b ca b ca b ca b ca b ca b c---"
    
  • #filter(filters) ⇒ Object

    Run the given filters in the order given.

    Examples:

    n = NeuS.new "abc"
    n.filter :spacial
    # => "a b c"
    n.filter :spacial, :dashes
    # => "---a b c---"

    Parameters:

    • filters (Array<#to_sym>)
  • #filter(filters_and_options) ⇒ Object

    Run the given filters in the order given with the given options. Pass any options for a filter at the end, by giving the options as a hash with the name of the filter. I’d prefer to give the filter name and the options at the same time, but the Ruby argument parser can’t handle it, which is fair enough :)

    Examples:

    n = NeuS.new "abc"
    n.filter :dashes, :spacial, :dashes => {some_option_for_dashes: true}


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/texttube/base.rb', line 139

def filter( *order_and_options )
  if order_and_options.last.respond_to?(:keys)
    *order,options = *order_and_options
  else
    order,options = order_and_options, {}
  end
  order = order.flatten
  order = @options.fetch :order, self.class.filters if order.empty?
  order.inject(self){|current,filter|
    send filter, current, options
  }
end

#filtersObject

See all added filters.



153
154
155
# File 'lib/texttube/base.rb', line 153

def filters
  self.class.filters
end