class Pachinko
Example format of a patch: class YourPatch < Pachinko
# name: any string naming this patch def name 'Example Patch Creating FabricatedClass' end # relevant?: a method that returns true only if the patch needs to be applied def relevant? !defined?(FabricatedClass) end # patch: a method which you pass a block which applies the patch # Reason for this is that you can't reopen classes in methods... patch do class ::FabricatedClass; end end
end
Attributes
last_patch[RW]
patch_block[R]
results[R]
Public Class Methods
development_mode?()
click to toggle source
# File lib/pachinko.rb, line 50 def development_mode? defined?(::Rails) && defined?(::Rails.env) && ::Rails.env.development? end
last_results()
click to toggle source
# File lib/pachinko.rb, line 62 def last_results last_patch.results end
mute?()
click to toggle source
# File lib/pachinko.rb, line 34 def self.mute? ENV['MUTE_PACHINKO'] end
patch(&block)
click to toggle source
# File lib/pachinko.rb, line 42 def patch(&block) @patch_block = block end
run(*args)
click to toggle source
# File lib/pachinko.rb, line 46 def run(*args) new.run(*args) end
test_mode?()
click to toggle source
# File lib/pachinko.rb, line 54 def test_mode? if defined?(::Rails) && defined?(::Rails.env) ::Rails.env.test? else true end end
Public Instance Methods
applied?()
click to toggle source
# File lib/pachinko.rb, line 30 def applied? @applied end
irrelevant?()
click to toggle source
Overridable in subclasses, although not recommended Should return true if the patch was applied (which in most cases means it’s also no longer applicable)
# File lib/pachinko.rb, line 79 def irrelevant? !relevant? end
name()
click to toggle source
# File lib/pachinko.rb, line 67 def name raise NotImplementedError, "Your patch doesn't define a 'name' method... please override in your patch class" end
relevant?()
click to toggle source
This method returns a boolean that actually checks the mechanics of whatever the patch is supposed to fix. It should return true if the patch still needs to be applied, and false after it has been applied or if it doesn’t need to be applied.
# File lib/pachinko.rb, line 73 def relevant? raise NotImplementedError, "Your patch doesn't define a 'relevant?' method which tests whether it is necessary... please override in your patch class" end
run(force_plain_output = false)
click to toggle source
# File lib/pachinko.rb, line 83 def run(force_plain_output = false) @force_plain_output = force_plain_output @success = false @applied = false output_msgs = [] output_msgs_warning = [] if relevant? apply @applied = true # Check to see if the patch is no longer needed (i.e., the test is valid, and the patch worked) if irrelevant? output_msgs << success_message @success = true else output_msgs_warning << relevancy_assertion_wrong_message end else output_msgs_warning << patch_not_applied_message end if !output_msgs.empty? && (Pachinko.development_mode? || force_plain_output) log_success output_msgs.join("\n") unless Pachinko.mute? end if !output_msgs_warning.empty? log_warning output_msgs_warning.join("\n") end Pachinko.last_patch = self @results = output_msgs_warning | output_msgs self end
success?()
click to toggle source
# File lib/pachinko.rb, line 26 def success? @success end
Private Instance Methods
ansi(color, text)
click to toggle source
# File lib/pachinko.rb, line 130 def ansi(color, text) if @force_plain_output # don't colorize text else ANSI.send(color){ text } end end
apply()
click to toggle source
# File lib/pachinko.rb, line 114 def apply begin self.class.patch_block.call rescue NameError => e e.message << "\nNOTE TO PATCH DEVS: If you are writing a patch, it is possible that you just have to root-namespace any relevant classes in your PATCH block with a double colon (::) in front, to avoid this error!" raise e end end
caution_prelude()
click to toggle source
# File lib/pachinko.rb, line 140 def caution_prelude "#{ansi :red, 'CAUTION'}: #{ansi :yellow, 'Patch'} #{ansi :bold, self.class.to_s} " end
log(out)
click to toggle source
# File lib/pachinko.rb, line 122 def log(out) unless out.empty? Rails.logger.info(out) if defined?(Rails) && defined?(Rails.logger) rescue nil puts out end end
Also aliased as: log_success, log_warning
patch_not_applied_message()
click to toggle source
# File lib/pachinko.rb, line 146 def patch_not_applied_message caution_prelude << "NOT applied! (test for necessity was false)" end
relevancy_assertion_wrong_message()
click to toggle source
# File lib/pachinko.rb, line 143 def relevancy_assertion_wrong_message caution_prelude << "either wasn't applied correctly or the test code for its necessity is wrong!" end
success_message()
click to toggle source
# File lib/pachinko.rb, line 137 def success_message "#{ansi :yellow, 'Patch'} #{ansi :bold, self.class.to_s} applied" end