Sha256: a374850de03196430c134eb167ef41808118735fccec5e23794888b7c6d415ba

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commit-msg-url-shortener-0.0.2 lib/commit-msg-url-shortener/service.rb
commit-msg-url-shortener-0.0.1 lib/commit-msg-url-shortener/service.rb