Class: Gemtronics::Grouper

Inherits:
Object
  • Object
show all
Defined in:
lib/gemtronics/grouper.rb

Overview

This class is yielded up when you create/modify a group. This class holds all the relevant information about the gems for the defined group.

See Gemtronics::Manager for more details on creating a group.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Grouper

Creates a new Gemtronics::Grouper class. It takes a Hash of options that will be applied to all gems added to the group. These options will be merged with Gemtronics::Manager::GLOBAL_DEFAULT_OPTIONS

This also takes a special option :dependencies which can be an Array of other groups to inherit gems from.

Example:

group(:development) do |g|
  g.add('gem1')
end

group(:test, :dependencies => :development) do |g|
  g.add('gem2')
  g.add('gem3')
end

In this example the :test group would now have the following gems: gem1, gem2, gem3



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gemtronics/grouper.rb', line 36

def initialize(name, options = {})
  self.name = name
  self.gems = []
  self.dependents = []
  options = {} if options.nil?
  deps = [options.delete(:dependencies)].flatten.compact
  self.group_options = Gemtronics::Manager::GLOBAL_DEFAULT_OPTIONS.merge(options)
  deps.each do |dep|
    self.dependency(dep)
  end
  self.group_options.merge!(options)
  self
end

Instance Attribute Details

#dependentsObject

An Array containing the names of the dependents of this group.



15
16
17
# File 'lib/gemtronics/grouper.rb', line 15

def dependents
  @dependents
end

#gemsObject

The Array of gems belonging to this group.



11
12
13
# File 'lib/gemtronics/grouper.rb', line 11

def gems
  @gems
end

#group_optionsObject

A Hash representing the default options for this group.



13
14
15
# File 'lib/gemtronics/grouper.rb', line 13

def group_options
  @group_options
end

#nameObject

The name of this group



9
10
11
# File 'lib/gemtronics/grouper.rb', line 9

def name
  @name
end

Instance Method Details

#add(name, options = {}) ⇒ Object

Adds a gem to the group. All that is required is the name of the gem.

The following is a list of the options that can be passed in:

:require # a String or Array of file(s) to be required by the system
:version # a String representing the version number of the gem.
:source # a String representing the source URL of where the gem lives
:load # true/false the files specified by the :require option should be loaded

These options get merged with the group options and the global options.

Example:

group(:default, :version => '>=1.0.0') do |g|
  g.add('gem1', :source => 'http://gems.example.com')
  g.add('gem2', :version => '0.9.8')
  g.add('gem3', :require => ['gem-three', 'gem3'], :version => '>2.0.0')
  g.add('gem4', :require => 'gemfour', :load => false)
end

# => [{:name => 'gem1', :version => '>=1.0.0', :source => 'http://gems.example.com', 
       :require => ['gem1'], :load => true},
      {:name => 'gem2', :version => '0.9.8', :source => 'http://gems.rubyforge.org', 
       :require => ['gem2'], :load => true},
      {:name => 'gem3', :version => '>2.0.0', :source => 'http://gems.rubyforge.org', 
       :require => ['gem-three', 'gem3'], :load => true},
      {:name => 'gem4', :version => '>=1.0.0', :source => 'http://gems.rubyforge.org', 
       :require => ['gemfour']}, :load => false]


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gemtronics/grouper.rb', line 76

def add(name, options = {})
  name = name.to_s
  ind = self.gems.size
  g = {}
  self.gems.each_with_index do |gemdef, i|
    if gemdef.name == name
      g = gemdef
      ind = i
      break
    end
  end

  g = Gemtronics::Definition[self.group_options.merge({:name => name, :require => [name]}.merge(g).merge(options))]
  self.gems[ind] = g
  self.dependents.each do |dep|
    Gemtronics.group(dep).add(name, options)
  end
end

#dependency(name) ⇒ Object

Injects another groups gems into this group.

Example:

group(:development) do |g|
  g.add('gem1')
end

group(:test) do |g|
  g.add('gem2')
  g.dependency(:development)
  g.add('gem3')
end

In this example the :test group would now have the following gems: gem2, gem1, gem3



135
136
137
138
139
140
141
142
143
144
# File 'lib/gemtronics/grouper.rb', line 135

def dependency(name)
  group = Gemtronics::Manager.instance.groups[name.to_sym]
  if group
    self.group_options.merge!(group.group_options)
    Gemtronics.group(name.to_sym).dependents << self.name
    group.gems.dup.each do |gemdef|
      self.add(gemdef.name, gemdef)
    end
  end
end

#remove(name) ⇒ Object

Removes a gem from the group.

Example:

group(:development) do |g|
  g.add('gem1')
  g.add('gem2')
end

group(:test, :dependencies => :development) do |g|
  g.add('gem3')
  g.remove('gem2')
  g.add('gem4')
end

In this example the :test group would now have the following gems: gem1, gem3, gem4



111
112
113
114
115
116
117
118
# File 'lib/gemtronics/grouper.rb', line 111

def remove(name)
  self.gems.each do |g|
    if g.name = name.to_s
      self.gems.delete(g)
      break
    end
  end
end

#search(name, options = {}) ⇒ Object

Finds and returns a Gemtronics::Definition for the specified gem, if one exists.

Example:

Gemtronics.group(:test).search('gem1')


151
152
153
154
155
156
# File 'lib/gemtronics/grouper.rb', line 151

def search(name, options = {})
  self.gems.each do |g|
    return g if g.name == name
  end
  return nil
end