Class: Footnotes::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-footnotes/footnotes.rb

Constant Summary collapse

@@no_style =
false
@@multiple_notes =
false
@@klasses =
[]
@@prefix =

Default link prefix is textmate

'txmt://open?url=file://%s&line=%d&column=%d'
@@notes =

Edit notes

[ :controller, :view, :layout, :stylesheets, :javascripts ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Filter

Returns a new instance of Filter.



90
91
92
93
94
95
# File 'lib/rails-footnotes/footnotes.rb', line 90

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

Class Method Details

.after(controller) ⇒ Object

Method that calls Footnotes to attach its contents



31
32
33
34
35
# File 'lib/rails-footnotes/footnotes.rb', line 31

def after(controller)
  filter = Footnotes::Filter.new(controller)
  filter.add_footnotes!
  filter.close!(controller)
end

.before(controller) ⇒ Object

Method called to start the notes It’s a before filter prepend in the controller



25
26
27
# File 'lib/rails-footnotes/footnotes.rb', line 25

def before(controller)
  Footnotes::Filter.start!(controller)
end

.each_with_rescue(notes) ⇒ Object

Process notes, discarding only the note if any problem occurs



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

def 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.camelize}Note Exception", e)
      delete_me << note
      next
    end
  end

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

.log_error(title, exception) ⇒ Object

Logs the error using specified title and format



73
74
75
# File 'lib/rails-footnotes/footnotes.rb', line 73

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

.prefix(*args) ⇒ Object

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



80
81
82
83
84
85
86
# File 'lib/rails-footnotes/footnotes.rb', line 80

def prefix(*args)
  if args.empty?
    @@prefix
  else
    format(@@prefix, *args)
  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



41
42
43
44
45
46
47
48
49
# File 'lib/rails-footnotes/footnotes.rb', line 41

def start!(controller)
  @@klasses = []

  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



97
98
99
100
101
102
# File 'lib/rails-footnotes/footnotes.rb', line 97

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



108
109
110
111
112
# File 'lib/rails-footnotes/footnotes.rb', line 108

def close!(controller)
  each_with_rescue(@@klasses) do |klass|
    klass.close!(controller)
  end
end