Class: GemfileLocker::GemfileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/gemfile_locker/gemfile_processor.rb

Direct Known Subclasses

Locker, Unlocker

Constant Summary collapse

GEM_LINE_REGEX =
/
  ^
  (?<prefix>\s*gem\s*["'])
  (?<name>[^'"]+)
  (?<name_quote>['"])
  (?<version_section>
    (?<version_prefix>\s*,\s*['"])
    (?<version>[^'"]*)
    (?<version_quote>['"])
  )?
  (?<suffix>,?.*)?
  $
/x
GEM_MATCH_FIELDS =
i(
  prefix
  name
  name_quote
  version_prefix
  version
  version_quote
  suffix
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GemfileProcessor

Returns a new instance of GemfileProcessor.



28
29
30
# File 'lib/gemfile_locker/gemfile_processor.rb', line 28

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/gemfile_locker/gemfile_processor.rb', line 26

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/gemfile_locker/gemfile_processor.rb', line 26

def path
  @path
end

Instance Method Details

#call(string) ⇒ Object



32
33
34
35
36
# File 'lib/gemfile_locker/gemfile_processor.rb', line 32

def call(string)
  process_gems(string) do |data|
    process_gem(data) unless skip_gem?(data)
  end
end

#process_gem(_name, _data) ⇒ Object



56
57
58
# File 'lib/gemfile_locker/gemfile_processor.rb', line 56

def process_gem(_name, _data)
  raise 'Abstract method'
end

#process_gems(string) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/gemfile_locker/gemfile_processor.rb', line 46

def process_gems(string)
  string.gsub(GEM_LINE_REGEX) do
    match = Regexp.last_match
    data = Hash[GEM_MATCH_FIELDS.map { |x| [x, match[x]] }]
    result = yield data
    result ||= data
    GEM_MATCH_FIELDS.map { |x| result[x] }.join
  end
end

#set_gem_version(data, version) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gemfile_locker/gemfile_processor.rb', line 60

def set_gem_version(data, version)
  data = data.dup
  if version
    data[:version_prefix] ||= ", #{data[:name_quote] || "'"}"
    data[:version_quote] ||= data[:name_quote] || "'"
    data[:version] = version
  else
    i(
      version_prefix
      version
      version_quote
    ).each { |x| data.delete(x) }
  end
  data
end

#skip_gem?(data) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/gemfile_locker/gemfile_processor.rb', line 38

def skip_gem?(data)
  if options[:only]
    !options[:only].include?(data[:name])
  elsif options[:except]
    options[:except].include?(data[:name])
  end
end