Class: DRG::Tasks::GemfileLine

Inherits:
Struct
  • Object
show all
Defined in:
lib/drg/tasks/gemfile_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indexObject Also known as: to_int

Returns the value of attribute index

Returns:

  • (Object)

    the current value of index



3
4
5
# File 'lib/drg/tasks/gemfile_line.rb', line 3

def index
  @index
end

#lineObject Also known as: to_s

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



3
4
5
# File 'lib/drg/tasks/gemfile_line.rb', line 3

def line
  @line
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/drg/tasks/gemfile_line.rb', line 3

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



7
8
9
# File 'lib/drg/tasks/gemfile_line.rb', line 7

def ==(other)
  line.to_s == other.to_s
end

#remove_versionString

Returns line.

Returns:

  • (String)

    line



18
19
20
# File 'lib/drg/tasks/gemfile_line.rb', line 18

def remove_version
  swap_version('')
end

#swap_version(full_version) ⇒ String

Returns line.

Examples:


line = "gem 'duck_puncher', '> 1', '< 2', require: false   # dude"
swap_version('1.5.5')
line # => "gem 'duck_puncher', '1.5.5', require: false   # dude"

Parameters:

  • full_version (String)

    is the quoted version to update or remove from the gem line

Returns:

  • (String)

    line



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/drg/tasks/gemfile_line.rb', line 32

def swap_version(full_version)
  code, *comments = line.split(/#/)
  parts = code.split(',')
  end_of_line = code.rstrip.end_with?(',') ? ",#{parts.last}" : ''
  if parts.size == 1
    parts[0].chomp!
    parts << " #{full_version}" unless full_version.empty?
  else
    # ignore the first part, which is the gem name
    parts_without_gem = parts.drop(1)
    # reject options hash
    version_parts = parts_without_gem.reject { |x| x.include?(':') }
    # remove all but the first version part from the original parts array
    version_parts.drop(1).each { |version_part| parts.delete(version_part) }
    # find the index of it
    if index = parts.index(version_parts.first)
      # replace the current gem version (inside quotes)
      parts[index].sub! /['"].+['"]/, full_version
    else
      parts.insert 1, " #{full_version}"
    end
    parts.reject! { |x| x.strip.empty? }
  end
  space_after_gem = parts.first[/\s+$/].to_s
  space_after_gem = space_after_gem[0, (space_after_gem.size - full_version.size) - 2] if space_after_gem.size > full_version.size
  parts.first.rstrip!
  line.replace parts.join(',')
  line << end_of_line
  line << space_after_gem << "#" << comments.join if comments.any?
  line << "\n" unless line.end_with?("\n")
  line
end

#update(new_version) ⇒ String

Returns line.

Parameters:

  • version (String)

    is the new value for the gem (add/replace)

Returns:

  • (String)

    line



13
14
15
# File 'lib/drg/tasks/gemfile_line.rb', line 13

def update(new_version)
  swap_version("'#{new_version.to_s}'")
end

#versionObject

Deprecated.
Note:

not used



67
68
69
# File 'lib/drg/tasks/gemfile_line.rb', line 67

def version
  line[/, (.+)\n?/, 1]
end