Class: GemfileSorter::GemExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/gemfile_sorter/gem_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gem, key) ⇒ GemExtractor

Returns a new instance of GemExtractor.



6
7
8
9
10
11
# File 'lib/gemfile_sorter/gem_extractor.rb', line 6

def initialize(gem, key)
  @gem = gem
  @key = key
  @running_pointer = nil
  @result = []
end

Instance Attribute Details

#gemObject (readonly)

Returns the value of attribute gem.



3
4
5
# File 'lib/gemfile_sorter/gem_extractor.rb', line 3

def gem
  @gem
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/gemfile_sorter/gem_extractor.rb', line 3

def key
  @key
end

#resultObject

Returns the value of attribute result.



4
5
6
# File 'lib/gemfile_sorter/gem_extractor.rb', line 4

def result
  @result
end

#running_pointerObject

Returns the value of attribute running_pointer.



4
5
6
# File 'lib/gemfile_sorter/gem_extractor.rb', line 4

def running_pointer
  @running_pointer
end

Instance Method Details

#annotated_resultObject



81
82
83
# File 'lib/gemfile_sorter/gem_extractor.rb', line 81

def annotated_result
  symbol_result? ? ":#{result_string}" : %("#{result_string}")
end

#extractObject



72
73
74
75
76
77
# File 'lib/gemfile_sorter/gem_extractor.rb', line 72

def extract
  return unless match?
  extract_initial_result
  extract_multiple_results
  result_string
end

#extract_initial_resultObject



67
68
69
70
# File 'lib/gemfile_sorter/gem_extractor.rb', line 67

def extract_initial_result
  self.running_pointer = starting_index + 1
  result << initial_result
end

#extract_multiple_resultsObject



32
33
34
35
36
37
38
39
# File 'lib/gemfile_sorter/gem_extractor.rb', line 32

def extract_multiple_results
  return unless result.first&.start_with?("[")
  loop do
    increment_pointer
    result << running_option
    break if result.last&.end_with?("]")
  end
end

#extraction_endObject



45
46
47
48
49
# File 'lib/gemfile_sorter/gem_extractor.rb', line 45

def extraction_end
  size = (result.last&.size || 0)
  size += 1 unless symbol_result?
  gem.line.index(result.last) + (size || 0) - 1
end

#extraction_startObject



41
42
43
# File 'lib/gemfile_sorter/gem_extractor.rb', line 41

def extraction_start
  gem.line.index(options[starting_index]) - 2
end

#increment_pointerObject



21
# File 'lib/gemfile_sorter/gem_extractor.rb', line 21

def increment_pointer = self.running_pointer += 1

#initial_resultObject



23
24
25
26
27
28
29
30
# File 'lib/gemfile_sorter/gem_extractor.rb', line 23

def initial_result
  value = running_option
  if value.match?("=>")
    increment_pointer
    value = running_option
  end
  value
end

#match?Boolean

Returns:

  • (Boolean)


17
# File 'lib/gemfile_sorter/gem_extractor.rb', line 17

def match? = !!starting_index

#optionsObject



13
# File 'lib/gemfile_sorter/gem_extractor.rb', line 13

def options = gem.options

#result_stringObject



58
59
60
61
62
63
64
65
# File 'lib/gemfile_sorter/gem_extractor.rb', line 58

def result_string
  result.map do |str|
    str.to_s
      .delete_prefix(":")
      .delete_prefix("[:")
      .delete_suffix("]")
  end.sort.join(", ")
end

#resulting_first_lineObject



85
86
87
# File 'lib/gemfile_sorter/gem_extractor.rb', line 85

def resulting_first_line
  "#{key} #{annotated_result} do\n"
end

#running_optionObject



19
# File 'lib/gemfile_sorter/gem_extractor.rb', line 19

def running_option = options[running_pointer]

#starting_indexObject



15
# File 'lib/gemfile_sorter/gem_extractor.rb', line 15

def starting_index = options.find_index { _1.match(/#{key}/) }

#symbol_result?Boolean

Returns:

  • (Boolean)


79
# File 'lib/gemfile_sorter/gem_extractor.rb', line 79

def symbol_result? = result.last&.include?(":")

#updated_lineObject



51
52
53
54
55
56
# File 'lib/gemfile_sorter/gem_extractor.rb', line 51

def updated_line
  return gem.line if result.empty?
  result = gem.line
  result[extraction_start..extraction_end] = ""
  "  #{result}"
end