Class: Footnotes::Filter

Inherits:
Object
  • Object
show all
Extended by:
EachWithRescue
Defined in:
lib/rails-footnotes/filter.rb

Constant Summary collapse

@@klasses =
[]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EachWithRescue

included

Constructor Details

#initialize(controller) ⇒ Filter

Returns a new instance of Filter.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rails-footnotes/filter.rb', line 73

def initialize(controller)
  @controller = controller
  @template = controller.instance_variable_get(:@template)
  @notes = []

  revert_pos(controller.response_body) do
    if controller.response.stream.respond_to?(:body)
      @body = controller.response.body
    end
  end
end

Class Method Details

.prefix(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails-footnotes/filter.rb', line 57

def prefix(*args)
  if args.empty?
    read_prefix
  else
    args.map! { |arg| arg.to_s.split("/").map{|s| ERB::Util.url_encode(s) }.join("/") }

    if read_prefix.respond_to? :call
      read_prefix.call(*args)
    else
      format(read_prefix, *args)
    end
  end
end

.read_prefixObject

If none argument is sent, simply return the prefix. Otherwise, replace the args in the prefix.



56
# File 'lib/rails-footnotes/filter.rb', line 56

alias_method :read_prefix, :prefix

.start!(controller) ⇒ Object

Calls the class method start! in each note Sometimes notes need to set variables or clean the environment to work properly This method allows this kind of setup



43
44
45
46
47
48
49
50
51
52
# File 'lib/rails-footnotes/filter.rb', line 43

def start!(controller)
  self.each_with_rescue(Footnotes.before_hooks) {|hook| hook.call(controller, self)}

  @@klasses = []
  self.each_with_rescue(notes.flatten) do |note|
    klass = "Footnotes::Notes::#{note.to_s.camelize}Note".constantize
    klass.start!(controller) if klass.respond_to?(:start!)
    @@klasses << klass
  end
end

Instance Method Details

#add_footnotes!Object



85
86
87
88
89
90
# File 'lib/rails-footnotes/filter.rb', line 85

def add_footnotes!
  add_footnotes_without_validation! if valid?
rescue Exception => e
  # Discard footnotes if there are any problems
  self.class.log_error("Footnotes Exception", e)
end

#close!(controller) ⇒ Object

Calls the class method close! in each note Sometimes notes need to finish their work even after being read This method allows this kind of work



96
97
98
99
# File 'lib/rails-footnotes/filter.rb', line 96

def close!(controller)
  self.each_with_rescue(@@klasses) {|klass| klass.close!(controller)}
  self.each_with_rescue(Footnotes.after_hooks) {|hook| hook.call(controller, self)}
end