Class: GemfileSorter::GemExtractor
- Inherits:
-
Object
- Object
- GemfileSorter::GemExtractor
- Defined in:
- lib/gemfile_sorter/gem_extractor.rb
Instance Attribute Summary collapse
-
#gem ⇒ Object
readonly
Returns the value of attribute gem.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#result ⇒ Object
Returns the value of attribute result.
-
#running_pointer ⇒ Object
Returns the value of attribute running_pointer.
Instance Method Summary collapse
- #annotated_result ⇒ Object
- #extract ⇒ Object
- #extract_initial_result ⇒ Object
- #extract_multiple_results ⇒ Object
- #extraction_end ⇒ Object
- #extraction_start ⇒ Object
- #increment_pointer ⇒ Object
- #initial_result ⇒ Object
-
#initialize(gem, key) ⇒ GemExtractor
constructor
A new instance of GemExtractor.
- #match? ⇒ Boolean
- #options ⇒ Object
- #result_string ⇒ Object
- #resulting_first_line ⇒ Object
- #running_option ⇒ Object
- #starting_index ⇒ Object
- #symbol_result? ⇒ Boolean
- #updated_line ⇒ Object
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
#gem ⇒ Object (readonly)
Returns the value of attribute gem.
3 4 5 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 3 def gem @gem end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 3 def key @key end |
#result ⇒ Object
Returns the value of attribute result.
4 5 6 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 4 def result @result end |
#running_pointer ⇒ Object
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_result ⇒ Object
81 82 83 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 81 def annotated_result symbol_result? ? ":#{result_string}" : %("#{result_string}") end |
#extract ⇒ Object
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_result ⇒ Object
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_results ⇒ Object
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_end ⇒ Object
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_start ⇒ Object
41 42 43 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 41 def extraction_start gem.line.index([starting_index]) - 2 end |
#increment_pointer ⇒ Object
21 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 21 def increment_pointer = self.running_pointer += 1 |
#initial_result ⇒ Object
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
17 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 17 def match? = !!starting_index |
#options ⇒ Object
13 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 13 def = gem. |
#result_string ⇒ Object
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_line ⇒ Object
85 86 87 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 85 def resulting_first_line "#{key} #{annotated_result} do\n" end |
#running_option ⇒ Object
19 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 19 def running_option = [running_pointer] |
#starting_index ⇒ Object
15 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 15 def starting_index = .find_index { _1.match(/#{key}/) } |
#symbol_result? ⇒ Boolean
79 |
# File 'lib/gemfile_sorter/gem_extractor.rb', line 79 def symbol_result? = result.last&.include?(":") |
#updated_line ⇒ Object
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 |