Class: Megam::WorkAreaLoader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/megam/workarea_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*workarea_repo_paths) ⇒ WorkAreaLoader

Returns a new instance of WorkAreaLoader.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/megam/workarea_loader.rb', line 12

def initialize(*workarea_repo_paths)
  workarea_repo_paths = workarea_repo_paths.flatten
  raise ArgumentError, "You must specify at least one workarea repo path" if workarea_repo_paths.empty?
  @workarea_by_name = Hashie::Mash.new
  @workarea_paths = Hash.new {|h,k| h[k] = []}
  @workarea_preconfig = Hash.new {|h,k| h[k] = []}
  @transferarea_paths = Hash.new {|h,k| h[k] = []}

  @workarea_repo_paths = workarea_repo_paths.map do |val|
    temp_val = File.join(Megam::WorkArea.workarea_install_directory,val)
     @workarea_preconfig[val] = temp_val
     val = temp_val
  end
  load_workarea_repos
end

Instance Attribute Details

#workarea_by_nameObject (readonly)

a Mash, indexed on name.



7
8
9
# File 'lib/megam/workarea_loader.rb', line 7

def workarea_by_name
  @workarea_by_name
end

#workarea_pathsObject (readonly)

Returns the value of attribute workarea_paths.



8
9
10
# File 'lib/megam/workarea_loader.rb', line 8

def workarea_paths
  @workarea_paths
end

#workarea_preconfigObject (readonly)

a hash the preconfig mapping eg: you => ~/megam_install/you



6
7
8
# File 'lib/megam/workarea_loader.rb', line 6

def workarea_preconfig
  @workarea_preconfig
end

Instance Method Details

#[](workarea) ⇒ Object Also known as: fetch



56
57
58
59
60
61
62
# File 'lib/megam/workarea_loader.rb', line 56

def [](workarea)
  if @workarea_by_name.has_key?(workarea.to_sym) or load_workarea_repo(workarea.to_sym)
  @workarea_by_name[workarea.to_sym]
  else
    raise Exceptions::WorkAreaNotFound, "Cannot find a workarea named #{workarea.to_s}; did you forget to add it in WORKAREA_INSTALL_DIR variable ?"
  end
end

#eachObject



72
73
74
75
76
# File 'lib/megam/workarea_loader.rb', line 72

def each
  @workarea_by_name.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |cname|
    yield(cname, @workarea_by_name[cname])
  end
end

#has_key?(workarea_name) ⇒ Boolean Also known as: key?

Returns:

  • (Boolean)


66
67
68
# File 'lib/megam/workarea_loader.rb', line 66

def has_key?(workarea_name)
  not self[workarea_name.to_sym].nil?
end

#load_workarea_repo(work_repo_name, repo_paths = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/megam/workarea_loader.rb', line 41

def load_workarea_repo(work_repo_name, repo_paths=nil)
  repo_paths ||= @workarea_repo_paths
  work_repo_key = nil

  repo_paths.each do |repo_path|
    workarea_repo = File.join(repo_path, work_repo_name.to_s)
    next unless File.directory?(workarea_repo) and Dir[File.join(repo_path, "**")].include?(workarea_repo)
    work_repo_key = workarea_repo.dup.gsub(Megam::WorkArea.workarea_install_directory + File::SEPARATOR ,"") #remove the extra slash
    @workarea_paths[work_repo_key] << workarea_repo
    @workarea_by_name[work_repo_key] = workarea_repo
  end
  @workarea_by_name[work_repo_key]
end

#load_workarea_reposObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/megam/workarea_loader.rb', line 28

def load_workarea_repos
  @workarea_repo_paths.each do |repo_path|
    Dir[File.join(repo_path, "*")].each do |first_level_path|
      load_workarea_repo(File.basename(first_level_path), [repo_path])        
    Dir[File.join(first_level_path, "*")].each do |work_repo_path|
      next unless File.directory?(work_repo_path)
      load_workarea_repo(File.basename(work_repo_path), [first_level_path])
    end
  end 
  end
  @workarea_by_name
end

#workarea_namesObject



78
79
80
# File 'lib/megam/workarea_loader.rb', line 78

def workarea_names
  @workarea_by_name.keys.sort
end