Class: RestoreFromRepository::TargetFile

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ TargetFile

Returns the target file instance.

Parameters:

  • text (String)

    the target file contents



55
56
57
# File 'lib/restore_from_repository/target_file.rb', line 55

def initialize(text)
  @body = text
end

Instance Attribute Details

#bodyString (readonly)

Returns file body.

Returns:

  • (String)

    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.

Parameters:

  • text (String)

    base target file

  • section (String)

    appending section

Returns:



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.

Parameters:

  • data (String)

    before restore

  • target_file (String) (defaults to: FILE_NAME)

    file name

  • pattern (Regexp) (defaults to: REGEX_BUNDLED_WITH)

    match pattern

  • ref (String) (defaults to: Repository::REF)

    git ref

  • git_path (String) (defaults to: Repository::GIT_PATH)

    git repository path

  • git_options (Hash) (defaults to: Repository::GIT_OPTIONS)

    ruby-git options

  • new_line (String) (defaults to: Repository::NEW_LINE)

    new line

Returns:

Raises:

  • (TypeError)


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,
  git_options = 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, git_options)
                     .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.

Parameters:

  • other (#body)

    compare body

Returns:

  • (Boolean)

    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.

Parameters:

  • pattern (Regexp, String)

    match pattern

Returns:



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.

Parameters:

  • pattern (Regexp)

    match pattern

Returns:

  • (String)

    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_sString

Returns the target file contents.

Returns:

  • (String)

    the target file contents



79
80
81
# File 'lib/restore_from_repository/target_file.rb', line 79

def to_s
  body
end