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



35
36
37
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 35

def add_line
  lines.insert(add_line_index, new_gem_line)
end

#add_line_indexObject



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

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)


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

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)


56
57
58
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 56

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

#gems_lines_start_indexInteger

Returns:

  • (Integer)


47
48
49
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 47

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

#new_gem_lineObject



51
52
53
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 51

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

#quoted_gem_specsObject



64
65
66
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 64

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

#replace_lineObject



60
61
62
# File 'lib/avm/eac_ruby_base1/rubygems/gemspec/add_or_replace_gem_line.rb', line 60

def replace_line
  lines[existing_gem_line_index] = new_gem_line
end

#resultObject



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

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