Class: AddGem

Inherits:
Object
  • Object
show all
Defined in:
lib/envGen/add_gem.rb

Constant Summary collapse

@@gems =

gets updated list of all current RubyGems gems

`gem list --remote`.split("\n")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ AddGem

input is gem name, called with @gemName



12
13
14
# File 'lib/envGen/add_gem.rb', line 12

def initialize(input) # input is gem name, called with @gemName
  @gemName = input
end

Instance Attribute Details

#gemNameObject

Returns the value of attribute gemName.



4
5
6
# File 'lib/envGen/add_gem.rb', line 4

def gemName
  @gemName
end

#gemsFoundObject

Returns the value of attribute gemsFound.



4
5
6
# File 'lib/envGen/add_gem.rb', line 4

def gemsFound
  @gemsFound
end

Class Method Details

.gemsObject



8
9
10
# File 'lib/envGen/add_gem.rb', line 8

def self.gems
  @@gems
end

Instance Method Details

#exactGem?(gemString) ⇒ Boolean

takes in a search term and a gem to match against

Returns:

  • (Boolean)


21
22
23
# File 'lib/envGen/add_gem.rb', line 21

def exactGem?(gemString) # takes in a search term and a gem to match against
  gemString.split(" ").first == gemName.downcase
end

#findPartialNamesObject

partial name search



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/envGen/add_gem.rb', line 55

def findPartialNames # partial name search
  gemsFound = []
  puts "Searching through #{@@gems.count} gems..."
  AddGem.gems.each do |gemString| # searching through gems + version numbers
    if exactGem?(gemString)
      gemsFound.unshift("** #{gemString}") # highlights exact match
    elsif gemString.include?(gemName.downcase)
      gemsFound << gemString
    end
  end
  gemsFound
end

#gemEntryObject

handles gem entry



45
46
47
48
49
50
51
52
53
# File 'lib/envGen/add_gem.rb', line 45

def gemEntry # handles gem entry
  if !inConfig? && gemExists?
    writeExactGem
  elsif inConfig? && gemExists? # add gem if doesn't exist
    puts "'#{gemName}' already added"
  else
    noExactGem
  end
end

#gemExists?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/envGen/add_gem.rb', line 25

def gemExists?
  @@gems.select {|gem| gem.split(" ").first == gemName }.count > 0
end

#gemSearchObject



68
69
70
71
72
73
74
75
76
# File 'lib/envGen/add_gem.rb', line 68

def gemSearch
  gemsFound = findPartialNames
  if gemsFound.count == 0
    puts "No exact match for '#{gemName.downcase}' found"
  else
    puts "\nFound #{gemsFound.count} gems:"
    gemsFound.each {|gem| puts gem}
  end
end

#inConfig?Boolean

looks through environment for gem

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/envGen/add_gem.rb', line 16

def inConfig? # looks through environment for gem
  # binding.pry
  File.readlines("config/environment.rb").grep("gem '#{gemName}'\n").count > 0
end

#noExactGemObject



40
41
42
43
# File 'lib/envGen/add_gem.rb', line 40

def noExactGem
  puts "No exact match for '#{gemName}' found"
  puts "Search for partial gem name with 'gem -s [gem]'"
end

#writeObject



29
30
31
32
33
# File 'lib/envGen/add_gem.rb', line 29

def write
  File.open("config/environment.rb", "a") {|env|
    env.puts "gem '#{gemName}'"
  }
end

#writeExactGemObject



35
36
37
38
# File 'lib/envGen/add_gem.rb', line 35

def writeExactGem
  write
  puts "Added '#{gemName}'"
end