Class: Avm::EacRubyBase1::Rubygems::Gemspec::AddOrReplaceGemLine

Inherits:
Object
  • Object
show all
Defined in:
lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb

Constant Summary collapse

DEPENDENCY_PREFIX =
'  s.add%<dependency_type>s_dependency \'%<gem_name>s\''

Instance Method Summary collapse

Instance Method Details

#add_lineObject



33
34
35
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 33

def add_line
  lines.insert(add_line_index, new_gem_line)
end

#add_line_indexObject



37
38
39
40
41
42
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 37

def add_line_index
  (gems_lines_start_index..(lines.count - 1)).each do |e|
    return e if new_gem_line < lines[e]
  end
  lines.count
end

#existing_gem_line_indexInteger

Returns:

  • (Integer)


18
19
20
21
22
23
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 18

def existing_gem_line_index
  lines.index do |line|
    ::Avm::EacRubyBase1::Rubygems::Gemspec::DEPENDENCY_LINE_PARSER.parse(line)
      .if_present(&:first) == gem_name
  end
end

#gem_line_prefixString

Returns:

  • (String)


54
55
56
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 54

def gem_line_prefix
  format(DEPENDENCY_PREFIX, { dependency_type: dependency_type, gem_name: gem_name })
end

#gems_lines_start_indexInteger

Returns:

  • (Integer)


45
46
47
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 45

def gems_lines_start_index
  existing_gem_line_index || (lines.count - 1)
end

#new_gem_lineObject



49
50
51
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 49

def new_gem_line
  ([gem_line_prefix] + quoted_gem_specs).join(', ')
end

#quoted_gem_specsObject



62
63
64
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 62

def quoted_gem_specs
  gem_specs.map { |gem_spec| "'#{gem_spec}'" }
end

#replace_lineObject



58
59
60
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 58

def replace_line
  lines[existing_gem_line_index] = new_gem_line
end

#resultObject



25
26
27
28
29
30
31
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 25

def result
  if existing_gem_line_index.present?
    replace_line
  else
    add_line
  end
end