Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Created August 4, 2010 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravinggenius/507531 to your computer and use it in GitHub Desktop.
Save ravinggenius/507531 to your computer and use it in GitHub Desktop.
  • A Filter is a Ruby module with a :process method that accepts text (like MarkDown) and filters it in some way. This is for a CMS. Filters do not get stored in the database.
  • A FilterGroup is (as it likely sounds) a list of Filters to apply to some piece of content. FilterGroups do get stored in the database.
  • Implemented FilterArray as suggested by http://groups.google.com/group/mongomapper/browse_thread/thread/4132688f57f41d17
class Filter
def initialize(name)
@name = name
end
def name
@name.gsub /\.rb/, ''
end
# this loads the filter module
def filter
@filter ||= name.camelize.constantize
end
def self.all
# ...
end
def self.process_all(input, filter_group)
filter_group.filters.each do |f|
input = f.filter.process input
end
input
end
end
class FilterArray < Array
def self.to_mongo(values)
values ||= []
values.map { |v| v.respond_to?(:name) ? v.name : v }
end
def self.from_mongo(values)
values ||= []
values.map { |v| v.respond_to?(:name) ? v : Filter["#{v}.rb"] }
end
end
class FilterGroup
include MongoMapper::Document
key :name, String, :required => true
key :filters, FilterArray, :required => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment