Class: RestoreBundledWith::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/restore_bundled_with/lock.rb

Overview

The lock file

Constant Summary collapse

REGEX_BUNDLED_WITH =
/^(?<pick>(?:\r\n|\r|\n)^BUNDLED WITH.*(?:\r\n|\r|\n).+(?:\r\n|\r|\n))/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Lock

Returns the lock file instance.

Parameters:

  • text (String)

    the lock file contents



50
51
52
# File 'lib/restore_bundled_with/lock.rb', line 50

def initialize(text)
  @body = text
end

Instance Attribute Details

#bodyString (readonly)

Returns file body.

Returns:

  • (String)

    file body



8
9
10
# File 'lib/restore_bundled_with/lock.rb', line 8

def body
  @body
end

Class Method Details

.insert(text, section) ⇒ Lock

Returns the lock file instance.

Parameters:

  • text (String)

    base lock file

  • section (String)

    appending section

Returns:

  • (Lock)

    the lock file instance



14
15
16
17
18
19
20
# File 'lib/restore_bundled_with/lock.rb', line 14

def self.insert(text, section)
  if section && !section.empty?
    new(text + section)
  else
    new(text)
  end
end

.restore(data, lockfile = Repository::LOCK_FILE, ref = Repository::REF, git_path = Repository::GIT_PATH, git_options = Repository::GIT_OPTIONS, new_line = Repository::NEW_LINE) ⇒ Lock

Returns the lock file instance.

Parameters:

  • data (String)

    before restore

  • lockfile (String) (defaults to: Repository::LOCK_FILE)

    file name

  • 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:

  • (Lock)

    the lock file instance



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/restore_bundled_with/lock.rb', line 30

def self.restore(
  data,
  lockfile = Repository::LOCK_FILE,
  ref = Repository::REF,
  git_path = Repository::GIT_PATH,
  git_options = Repository::GIT_OPTIONS,
  new_line = Repository::NEW_LINE
)
  trimmed = new(data).delete_bundled_with
  lock_file_data = Repository
                   .new(git_path, git_options)
                   .fetch_file(lockfile, ref, new_line)
  section = new(lock_file_data)
            .pick
  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



80
81
82
# File 'lib/restore_bundled_with/lock.rb', line 80

def ==(other)
  body == other.body
end

#delete_bundled_withLock

Returns new lock file instance which is deleted bundled with.

Examples:

delete bundled with

"\n\nBUNDLED WITH\n   1.10.4\n" => "\n"

Returns:

  • (Lock)

    new lock file instance which is deleted bundled with



58
59
60
# File 'lib/restore_bundled_with/lock.rb', line 58

def delete_bundled_with
  self.class.new(body.sub(REGEX_BUNDLED_WITH) { '' })
end

#pickString

Returns pick target section.

Returns:

  • (String)

    pick target section



63
64
65
66
67
68
69
70
# File 'lib/restore_bundled_with/lock.rb', line 63

def pick
  match = REGEX_BUNDLED_WITH.match(body)
  if match
    match[:pick]
  else
    ''
  end
end

#to_sString

Returns the lock file contents.

Returns:

  • (String)

    the lock file contents



73
74
75
# File 'lib/restore_bundled_with/lock.rb', line 73

def to_s
  body
end