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

'%.%.%'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FileUpdateRule.

Parameters:



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

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 Attribute Details

#after_patternObject (readonly)

Returns the value of attribute after_pattern.



6
7
8
# File 'lib/bump/domain/file_update_rule.rb', line 6

def after_pattern
  @after_pattern
end

#before_patternObject (readonly)

Returns the value of attribute before_pattern.



6
7
8
# File 'lib/bump/domain/file_update_rule.rb', line 6

def before_pattern
  @before_pattern
end

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/bump/domain/file_update_rule.rb', line 6

def file
  @file
end

Instance Method Details

#file_existsBoolean

Returns true if the file exists

Returns:

  • (Boolean)


33
34
35
# File 'lib/bump/domain/file_update_rule.rb', line 33

def file_exists
  File.exist? @file
end

#file_get_contentsString

Gets the contents of the file

Returns:

  • (String)


27
28
29
# File 'lib/bump/domain/file_update_rule.rb', line 27

def file_get_contents
  File.read @file, encoding: Encoding::UTF_8
end

#pattern_existsBoolean

Checks if the pattern found in the file

Returns:

  • (Boolean)


39
40
41
# File 'lib/bump/domain/file_update_rule.rb', line 39

def pattern_exists
  !file_get_contents.index(@before_pattern).nil?
end

#performvoid

This method returns an undefined value.

Performs file update



45
46
47
# File 'lib/bump/domain/file_update_rule.rb', line 45

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