Class: GemfileSorter::BlockMap

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

Direct Known Subclasses

Groups, Sources

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlockMap

Returns a new instance of BlockMap.



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

def initialize
  @blocks = {}
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

Instance Method Details

#add(names, line:, line_number:) ⇒ Object



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

def add(names, line:, line_number:)
  block = block(names, line:, line_number:)
  blocks[block.normalized_name] ||= block
end

#add_gem(gem) ⇒ Object



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

def add_gem(gem)
  add(gem.inline_result, line: gem.inline_line, line_number: gem.line_number)
end

#empty?Boolean

Returns:

  • (Boolean)


9
# File 'lib/gemfile_sorter/block_map.rb', line 9

def empty? = blocks.empty?

#extra_line_unless_emptyObject



11
12
13
# File 'lib/gemfile_sorter/block_map.rb', line 11

def extra_line_unless_empty
  empty? ? "" : "\n"
end

#to_sObject



24
25
26
27
# File 'lib/gemfile_sorter/block_map.rb', line 24

def to_s
  extra_line_unless_empty +
    blocks.values.sort.map { _1.to_s }.join.delete_prefix("\n")
end