Created
February 8, 2010 11:14
-
-
Save joeljunstrom/298058 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spock | |
module Base | |
module Filterable | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def filter(f) | |
f_name, f_arguments = f.to_s.split(':') | |
f_arguments = f_arguments.to_s.split(',') | |
if f_name && available_filters.include?(f_name.to_sym) | |
f_arguments.empty? ? send("filter_#{f_name}".to_sym) : self.scopes["filter_#{f_name}".to_sym].call(self, *f_arguments) | |
else | |
self | |
end | |
end | |
def available_filters | |
self.scopes.keys.map { |m| m.to_s }.select do |m| | |
m =~ /^filter_/ | |
end.map { |m| m[7..-1].to_sym } | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment