Class: GemfileSorter::Line::Gem

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/gemfile_sorter/line/gem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, comment_group = nil, *options, line: "", line_number: nil) ⇒ Gem

Returns a new instance of Gem.



8
9
10
11
12
13
14
15
16
17
# File 'lib/gemfile_sorter/line/gem.rb', line 8

def initialize(name, comment_group = nil, *options, line: "", line_number: nil)
  @line = line.rstrip
  @name = name
  @options = options
  @line_number = line_number
  @comment_group = comment_group || CommentGroup.new
  @group_extractor = GemExtractor.new(self, "group")
  @source_extractor = GemExtractor.new(self, "source")
  @parsed = false
end

Instance Attribute Details

#comment_groupObject

Returns the value of attribute comment_group.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def comment_group
  @comment_group
end

#group_extractorObject (readonly)

Returns the value of attribute group_extractor.



5
6
7
# File 'lib/gemfile_sorter/line/gem.rb', line 5

def group_extractor
  @group_extractor
end

#lineObject

Returns the value of attribute line.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def line
  @line
end

#line_numberObject

Returns the value of attribute line_number.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def options
  @options
end

#parsedObject

Returns the value of attribute parsed.



6
7
8
# File 'lib/gemfile_sorter/line/gem.rb', line 6

def parsed
  @parsed
end

#source_extractorObject (readonly)

Returns the value of attribute source_extractor.



5
6
7
# File 'lib/gemfile_sorter/line/gem.rb', line 5

def source_extractor
  @source_extractor
end

Instance Method Details

#<=>(other) ⇒ Object



47
48
49
50
51
# File 'lib/gemfile_sorter/line/gem.rb', line 47

def <=>(other)
  name <=> other.name
rescue
  raise InvalidLineComparisonError
end

#extractObject



36
37
38
39
# File 'lib/gemfile_sorter/line/gem.rb', line 36

def extract
  self.parsed = true
  group_extractor.extract || source_extractor.extract
end

#inline_kindObject



41
# File 'lib/gemfile_sorter/line/gem.rb', line 41

def inline_kind = inline_match&.key

#inline_lineObject



45
# File 'lib/gemfile_sorter/line/gem.rb', line 45

def inline_line = inline_match.resulting_first_line

#inline_matchObject



28
29
30
31
32
# File 'lib/gemfile_sorter/line/gem.rb', line 28

def inline_match
  extract unless parsed
  [group_extractor, source_extractor]
    .find { _1.match? }
end

#inline_match?Boolean

Returns:

  • (Boolean)


34
# File 'lib/gemfile_sorter/line/gem.rb', line 34

def inline_match? = !!inline_match

#inline_resultObject



43
# File 'lib/gemfile_sorter/line/gem.rb', line 43

def inline_result = inline_match&.result_string

#parsed_lineObject



23
24
25
26
# File 'lib/gemfile_sorter/line/gem.rb', line 23

def parsed_line
  return line unless inline_match?
  inline_match.updated_line
end

#to_sObject



19
20
21
# File 'lib/gemfile_sorter/line/gem.rb', line 19

def to_s
  (comment_group.empty? ? "" : "\n") + comment_group.to_s + parsed_line + "\n"
end