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, :partials, :stylesheets, :javascripts ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Filter

Returns a new instance of Filter.



100
101
102
103
104
105
# File 'lib/rails-footnotes/footnotes.rb', line 100

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

Class Method Details

.each_with_rescue(collection) ⇒ Object

Process notes, discarding only the note if any problem occurs



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails-footnotes/footnotes.rb', line 63

def each_with_rescue(collection)
  delete_me = []

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

  delete_me.each { |item| collection.delete(item) }
  return collection
end

.log_error(title, exception) ⇒ Object

Logs the error using specified title and format



83
84
85
# File 'lib/rails-footnotes/footnotes.rb', line 83

def log_error(title, exception)
  Rails.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.



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

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



50
51
52
53
54
55
56
57
58
59
# File 'lib/rails-footnotes/footnotes.rb', line 50

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



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

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



118
119
120
121
# File 'lib/rails-footnotes/footnotes.rb', line 118

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