Class: GemfileSorter::BlockOfGems

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

Direct Known Subclasses

Line::Group, Line::Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, line:, line_number:) ⇒ BlockOfGems

Returns a new instance of BlockOfGems.



5
6
7
8
9
# File 'lib/gemfile_sorter/block_of_gems.rb', line 5

def initialize(name, line:, line_number:)
  @gems = {}
  @line = line
  @line_number = line_number
end

Instance Attribute Details

#gemsObject

Returns the value of attribute gems.



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

def gems
  @gems
end

#lineObject

Returns the value of attribute line.



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

def line
  @line
end

#line_numberObject

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

Instance Method Details

#add(gem) ⇒ Object



15
16
17
# File 'lib/gemfile_sorter/block_of_gems.rb', line 15

def add(gem)
  @gems[gem.name] = gem
end

#container?Boolean

Returns:

  • (Boolean)


11
# File 'lib/gemfile_sorter/block_of_gems.rb', line 11

def container? = false

#empty?Boolean

Returns:

  • (Boolean)


35
# File 'lib/gemfile_sorter/block_of_gems.rb', line 35

def empty? = gems.empty?

#gem_stringObject



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

def gem_string
  gems.values.sort.map { _1.to_s }.join.delete_prefix("\n")
end

#inside_block_mapObject



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

def inside_block_map = nil

#leading_spacesObject



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

def leading_spaces
  line.match(/(\A *)/)[1]
end

#to_sObject



27
28
29
30
31
32
33
# File 'lib/gemfile_sorter/block_of_gems.rb', line 27

def to_s
  "\n" +
    line +
    gem_string.delete_suffix(",") +
    (inside_block_map&.to_s || "") +
    "#{leading_spaces}end\n"
end