module CommitMsgUrlShortener class Service attr_reader :name def initialize name, &block @name = name @block = block end def apply commit_msg_filepath raise "Cannot find temporary commit message file #{commit_msg_filepath.inspect}" unless File.exist? commit_msg_filepath commit_message = File.read(commit_msg_filepath).strip transformed_message = ServiceHelper.module_exec(commit_message, &@block) || '' transformed_message.strip! if transformed_message != commit_message and transformed_message.size > 0 File.open(commit_msg_filepath, 'wb') {|file| file.write transformed_message} transformed_message else nil end end def self.define &block if @@expected.nil? or @@expected.kind_of?(Service) raise "You must load the service script using the Service#fabricate method." end @@expected = Service.new @@expected, &block end def self.fabricate path @@expected = name_of path require path unless @@expected.kind_of? Service puts "Warning! Service #{@@expected.inspect} should call the Service#define method." else @@expected end end def self.name_of path File.basename(path).gsub(File.extname(path), '').downcase.strip end end end