Module: BeforeFilters

Defined in:
lib/before_filters.rb,
lib/before_filters/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#before_filter(method_name, options = {}) ⇒ Object

DSL method that classes use to add before filters

Raises:

  • (StandardError)


17
18
19
20
21
# File 'lib/before_filters.rb', line 17

def before_filter(method_name, options = {})
  raise StandardError, "List of methods must be a whitelist"  unless options.has_key?(:only)
  raise StandardError, "List of methods must be an array"     unless options[:only].kind_of?(Array)
  filters[method_name] = options[:only]
end

#filtersObject

keeps track of all before filters



24
25
26
# File 'lib/before_filters.rb', line 24

def filters
  @filters ||= {}
end

#method_added(method_name) ⇒ Object

method that is invoked when a new instance method is added to class



6
7
8
9
10
11
12
13
14
# File 'lib/before_filters.rb', line 6

def method_added(method_name)

  # do not add to filer chain on a method if it already exists on the method
  return unless self.instance_methods(false).include?(method_name)
  return if filters.keys.include?(method_name) || filtered_methods.include?(method_name)
  return unless filters.keys.select { |key| filters[key].include?(instance_method(method_name).name) }

  add_filters_to(method_name)
end