Class: RestoreFromRepository::TargetFile
- Inherits:
-
Object
- Object
- RestoreFromRepository::TargetFile
- Defined in:
- lib/restore_from_repository/target_file.rb
Overview
The target file
Constant Summary collapse
- REGEX_BUNDLED_WITH =
/^(?<pick>(?:\r\n|\r|\n)^BUNDLED WITH.*(?:\r\n|\r|\n).+(?:\r\n|\r|\n))/
- FILE_NAME =
'Gemfile.lock'.freeze
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
File body.
Class Method Summary collapse
-
.insert(text, section) ⇒ TargetFile
The target file instance.
-
.restore(data, target_file = FILE_NAME, pattern = REGEX_BUNDLED_WITH, ref = Repository::REF, git_path = Repository::GIT_PATH, git_options = Repository::GIT_OPTIONS, new_line = Repository::NEW_LINE) ⇒ TargetFile
The target file instance.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if file body is same.
-
#delete_by_pattern(pattern) ⇒ TargetFile
New target file instance.
-
#initialize(text) ⇒ TargetFile
constructor
The target file instance.
-
#pick_by_pattern(pattern) ⇒ String
Picked section.
-
#to_s ⇒ String
The target file contents.
Constructor Details
#initialize(text) ⇒ TargetFile
Returns the target file instance.
55 56 57 |
# File 'lib/restore_from_repository/target_file.rb', line 55 def initialize(text) @body = text end |
Instance Attribute Details
#body ⇒ String (readonly)
Returns file body.
8 9 10 |
# File 'lib/restore_from_repository/target_file.rb', line 8 def body @body end |
Class Method Details
.insert(text, section) ⇒ TargetFile
Returns the target file instance.
15 16 17 18 19 20 21 |
# File 'lib/restore_from_repository/target_file.rb', line 15 def self.insert(text, section) if section && !section.empty? new(text + section) else new(text) end end |
.restore(data, target_file = FILE_NAME, pattern = REGEX_BUNDLED_WITH, ref = Repository::REF, git_path = Repository::GIT_PATH, git_options = Repository::GIT_OPTIONS, new_line = Repository::NEW_LINE) ⇒ TargetFile
Returns the target file instance.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/restore_from_repository/target_file.rb', line 32 def self.restore( data, target_file = FILE_NAME, pattern = REGEX_BUNDLED_WITH, ref = Repository::REF, git_path = Repository::GIT_PATH, = Repository::GIT_OPTIONS, new_line = Repository::NEW_LINE ) raise TypeError if target_file.nil? trimmed = new(data).delete_by_pattern(pattern) target_file_data = Repository .new(git_path, ) .fetch_file(target_file, ref, new_line) section = new(target_file_data) .pick_by_pattern(pattern) insert(trimmed.body, section) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if file body is same.
86 87 88 |
# File 'lib/restore_from_repository/target_file.rb', line 86 def ==(other) body == other.body end |
#delete_by_pattern(pattern) ⇒ TargetFile
Returns new target file instance.
62 63 64 |
# File 'lib/restore_from_repository/target_file.rb', line 62 def delete_by_pattern(pattern) self.class.new(body.sub(pattern) { '' }) end |
#pick_by_pattern(pattern) ⇒ String
Returns picked section.
69 70 71 72 73 74 75 76 |
# File 'lib/restore_from_repository/target_file.rb', line 69 def pick_by_pattern(pattern) match = pattern.match(body) if match match[:pick] else '' end end |
#to_s ⇒ String
Returns the target file contents.
79 80 81 |
# File 'lib/restore_from_repository/target_file.rb', line 79 def to_s body end |