Class: RestoreBundledWith::Lock

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

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 a new instance of Lock.



32
33
34
# File 'lib/restore_bundled_with/lock.rb', line 32

def initialize(text)
  @body = text
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/restore_bundled_with/lock.rb', line 3

def body
  @body
end

Class Method Details

.insert(text, section) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/restore_bundled_with/lock.rb', line 7

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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/restore_bundled_with/lock.rb', line 15

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) ⇒ Object



54
55
56
# File 'lib/restore_bundled_with/lock.rb', line 54

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

#delete_bundled_withObject

“nnBUNDLED WITHn 1.10.4n” => “n”



37
38
39
# File 'lib/restore_bundled_with/lock.rb', line 37

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

#pickObject



41
42
43
44
45
46
47
48
# File 'lib/restore_bundled_with/lock.rb', line 41

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

#to_sObject



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

def to_s
  body
end