Class: Bump::FileUpdateRule

Inherits:
Object
  • Object
show all
Defined in:
lib/bump/domain/file_update_rule.rb

Overview

The file update rule model

is able to perform actual file update

Constant Summary collapse

PLACEHOLDER_PATTERN =

The placeholder pattern

'%.%.%'

Instance Method Summary collapse

Constructor Details

#initialize(file, pattern, before_version, after_version) ⇒ FileUpdateRule



17
18
19
20
21
22
23
24
# File 'lib/bump/domain/file_update_rule.rb', line 17

def initialize(file, pattern, before_version, after_version)
    @file = file
    @pattern = pattern || PLACEHOLDER_PATTERN # default pattern is '%.%.%'
    @before_version = before_version
    @after_version = after_version
    @before_pattern = @pattern.sub PLACEHOLDER_PATTERN, @before_version
    @after_pattern = @pattern.sub PLACEHOLDER_PATTERN, @after_version
end

Instance Method Details

#afterPatternString

Gets the version string after bumping



43
44
45
# File 'lib/bump/domain/file_update_rule.rb', line 43

def afterPattern
    @after_pattern
end

#beforePatternString

Gets the version string before bumping



36
37
38
# File 'lib/bump/domain/file_update_rule.rb', line 36

def beforePattern
    @before_pattern
end

#fileString

Gets the file name



29
30
31
# File 'lib/bump/domain/file_update_rule.rb', line 29

def file
    @file
end

#fileExistsBoolean

Returns true if the file exists



56
57
58
# File 'lib/bump/domain/file_update_rule.rb', line 56

def fileExists
    File.exist? @file
end

#fileGetContentsString

Gets the contents of the file



50
51
52
# File 'lib/bump/domain/file_update_rule.rb', line 50

def fileGetContents
    File.read @file, :encoding => Encoding::UTF_8
end

#patternExistsBoolean

Checks if the pattern found in the file



63
64
65
# File 'lib/bump/domain/file_update_rule.rb', line 63

def patternExists
    fileGetContents.index(@before_pattern) != nil
end

#performvoid

This method returns an undefined value.

Performs file update



70
71
72
# File 'lib/bump/domain/file_update_rule.rb', line 70

def perform
    File.write @file, fileGetContents.sub(@before_pattern, @after_pattern)
end