Class: CommitMsgUrlShortener::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-msg-url-shortener/service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Service

Returns a new instance of Service.



4
5
6
7
# File 'lib/commit-msg-url-shortener/service.rb', line 4

def initialize name, &block
  @name  = name
  @block = block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/commit-msg-url-shortener/service.rb', line 3

def name
  @name
end

Class Method Details

.define(&block) ⇒ Object



22
23
24
25
26
27
# File 'lib/commit-msg-url-shortener/service.rb', line 22

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

.fabricate(path) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/commit-msg-url-shortener/service.rb', line 29

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

.name_of(path) ⇒ Object



39
40
41
# File 'lib/commit-msg-url-shortener/service.rb', line 39

def self.name_of path
  File.basename(path).gsub(File.extname(path), '').downcase.strip
end

Instance Method Details

#apply(commit_msg_filepath) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/commit-msg-url-shortener/service.rb', line 9

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