Class: Bump::FileUpdateRule
- Inherits:
-
Object
- Object
- Bump::FileUpdateRule
- 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
-
#after_pattern ⇒ Object
readonly
Returns the value of attribute after_pattern.
-
#before_pattern ⇒ Object
readonly
Returns the value of attribute before_pattern.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
-
#file_exists ⇒ Boolean
Returns true if the file exists.
-
#file_get_contents ⇒ String
Gets the contents of the file.
-
#initialize(file, pattern, before_version, after_version) ⇒ FileUpdateRule
constructor
A new instance of FileUpdateRule.
-
#pattern_exists ⇒ Boolean
Checks if the pattern found in the file.
-
#perform ⇒ void
Performs file update.
Constructor Details
#initialize(file, pattern, before_version, after_version) ⇒ FileUpdateRule
Returns a new instance of FileUpdateRule.
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_pattern ⇒ Object (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_pattern ⇒ Object (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 |
#file ⇒ Object (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_exists ⇒ Boolean
Returns true if the file exists
33 34 35 |
# File 'lib/bump/domain/file_update_rule.rb', line 33 def file_exists File.exist? @file end |
#file_get_contents ⇒ String
Gets the contents of the file
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_exists ⇒ Boolean
Checks if the pattern found in the file
39 40 41 |
# File 'lib/bump/domain/file_update_rule.rb', line 39 def pattern_exists !file_get_contents.index(@before_pattern).nil? end |
#perform ⇒ void
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 |