Class: ComponentPaths

Inherits:
Object show all
Defined in:
lib/volt/server/rack/component_paths.rb

Overview

ComponentPaths gives an array of every folder where you find a component.

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ ComponentPaths

Returns a new instance of ComponentPaths.



3
4
5
# File 'lib/volt/server/rack/component_paths.rb', line 3

def initialize(root=nil)
  @root = root || Dir.pwd
end

Instance Method Details

#app_foldersObject

Yield for every folder where we might find components



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/volt/server/rack/component_paths.rb', line 8

def app_folders
  # Find all app folders
  @app_folders ||= begin
    app_folders = ["#{@root}/app", "#{@root}/vendor/app"].map {|f| File.expand_path(f) }
    
    # Gem folders with volt in them
    # TODO: we should probably qualify this a bit more
    app_folders += Gem.loaded_specs.values.map { |g| g.full_gem_path }.reject {|g| g !~ /volt/ }.map {|f| f + '/app' }

    app_folders
  end
  
  # Yield each app folder and return a flattened array with
  # the results
  
  files = []
  @app_folders.each do |app_folder|
    files << yield(app_folder)
  end
  
  return files.flatten
end

#asset_foldersObject

Return every asset folder we need to serve from



73
74
75
76
77
78
79
80
81
82
# File 'lib/volt/server/rack/component_paths.rb', line 73

def asset_folders
  folders = []
  app_folders do |app_folder|
    Dir["#{app_folder}/*/assets"].sort.each do |asset_folder|
      folders << yield(asset_folder)
    end
  end
  
  folders.flatten
end

#component_path(name) ⇒ Object

Returns the path for a specific component



62
63
64
65
66
67
68
69
70
# File 'lib/volt/server/rack/component_paths.rb', line 62

def component_path(name)
  folders = components[name]
  
  if folders
    return folders.first
  else
    return nil
  end
end

#componentsObject

returns an array of every folder that is a component



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/volt/server/rack/component_paths.rb', line 32

def components
  return @components if @components
  
  @components = {}
  app_folders do |app_folder|
    Dir["#{app_folder}/*"].sort.each do |folder|
      if File.directory?(folder)
        folder_name = folder[/[^\/]+$/]
        
        @components[folder_name] ||= []
        @components[folder_name] << folder          
      end
    end
  end
  
  return @components
end

#setup_components_load_pathObject

Makes each components classes available on the load path



51
52
53
54
55
56
57
58
59
# File 'lib/volt/server/rack/component_paths.rb', line 51

def setup_components_load_path
  components.each do |name,component_folders|
    component_folders.each do |component_folder|
      Dir["#{component_folder}/tasks"].sort.each do |tasks_folder|
        $LOAD_PATH.unshift(tasks_folder)
      end
    end
  end
end