Class: MerbFootnotes::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_footnotes/filter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Filter

Returns a new instance of Filter.



53
54
55
56
57
# File 'lib/merb_footnotes/filter.rb', line 53

def initialize(controller)
  @controller = controller
  @body = controller.body
  @notes = []
end

Class Method Details

.each_with_rescue(notes) ⇒ Object

Process notes, discarding only the note if any problem occurs



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/merb_footnotes/filter.rb', line 30

def self.each_with_rescue(notes)
  delete_me = []

  notes.each do |note|
    begin
      yield note
    rescue Exception => e
      # Discard note if it has a problem
      log_error("Footnotes #{note.to_s.camel_case}Note Exception", e)
      delete_me << note
      next
    end
  end

  delete_me.each{ |note| notes.delete(note) }
end

.log_error(title, exception) ⇒ Object

Logs the error using specified title and format



49
50
51
# File 'lib/merb_footnotes/filter.rb', line 49

def self.log_error(title, exception)
  Merb.logger.error "#{title}: #{exception}\n#{exception.backtrace.join("\n")}"
end

.notesObject



4
5
6
7
8
9
10
11
12
13
# File 'lib/merb_footnotes/filter.rb', line 4

def self.notes
  @@notes ||= begin
    note_list = Merb::Plugins.config[:merb_footnotes][:notes].to_a
    if Merb::Plugins.config[:merb_footnotes][:append_notes].is_a? Array
      # Use custom list if set
      note_list += Merb::Plugins.config[:merb_footnotes][:append_notes]
    end
    note_list.flatten.compact
  end
end

.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



19
20
21
22
23
24
25
26
# File 'lib/merb_footnotes/filter.rb', line 19

def self.start!(controller)
  each_with_rescue(self.notes) do |note|
    klass = eval("MerbFootnotes::Notes::#{note.to_s.camel_case}Note") if note.is_a?(Symbol) || note.is_a?(String)
    if klass.respond_to?(:start!)
      klass.start!(controller) 
    end
  end
end

Instance Method Details

#add_footnotes!Object



59
60
61
62
63
64
# File 'lib/merb_footnotes/filter.rb', line 59

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



70
71
72
73
74
# File 'lib/merb_footnotes/filter.rb', line 70

def close!(controller)
  each_with_rescue(@notes) do |note|
    note.class.close!(controller)
  end
end