Module: Filters

Defined in:
lib/dm-filters.rb

Overview

This module enables a property to be filtered on save into another property, using per-row and/or per-property filters. The syntax when defining a property is:

property :prop_name, :filter => {:to => :filtered_prop, :with => :filter_column, :default => "DefaultFilter"}
(:with and :default are optional, though at least one should be specified.)

If the properties in :to and :with have not yet been defined, they will be defined automatically. Hence, if want to specify any options with this, they should be defined before to filtered property.

Defined Under Namespace

Modules: Resource

Constant Summary collapse

VERSION =
"0.4.0"
AVAILABLE_FILTERS =

A hash, with each entry of the form: Filter name (used in filters property) =>

Array of two elements Arrays representing classes that can process this

filter. Elements are: [ argument_for_require, class_name ]. Classes are assumed to respond to to_html.

{
  'Smartypants' => [['rubypants', 'RubyPants']],
  'Markdown' => [['maruku', 'Maruku'], ['rdiscount', 'RDiscount'], ['bluecloth', 'BlueCloth']],
  'Textile' => [['redcloth', 'RedCloth']],
  'BibleML' => [[File.dirname(__FILE__) + '/filters/bible_ml', 'BibleML']],
  'Linebreaker' => [[File.dirname(__FILE__) + '/filters/linebreaker', 'Linebreaker']],
}

Class Method Summary collapse

Class Method Details

.get_filter(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dm-filters.rb', line 39

def self.get_filter(name)
  # Camelize name; copied from ActiveSupport's #camelize
  name = name.strip.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  info = AVAILABLE_FILTERS[name]
  return(nil) unless info
    
  # Try to find loaded class
  info.each do |c|
    return Object.const_get(c[1]) if Object.const_defined?(c[1])
  end
  
  # Try to require a class
  info.each do |c|
    begin
      require(c[0])
      return Object.const_get(c[1])
    rescue LoadError
      # Try next
    end
  end
  
  return nil
end

.process(filters, content) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dm-filters.rb', line 27

def self.process(filters, content)
  return content if filters.nil?
  
  filters = filters.split(';') if filters.kind_of?(String)
  filters.each do |name|
    filter = get_filter(name)
    next unless filter
    content = filter.new(content).to_html
  end
  content
end