Class: Volt::ComponentPaths

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

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ ComponentPaths

Returns a new instance of ComponentPaths.



6
7
8
# File 'lib/volt/server/rack/component_paths.rb', line 6

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

Instance Method Details

#app_foldersObject

Yield for every folder where we might find components



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

def app_folders
  # Find all app folders
  @app_folders ||= begin
    volt_app    = File.expand_path(File.join(File.dirname(__FILE__), '../../../../app'))
    app_folders = [volt_app, "#{@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.reduce([]) { |paths, gem| paths << "#{gem.full_gem_path}/app" if gem.name =~ /volt/; paths }

    app_folders.uniq
  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

  files.flatten
end

#asset_foldersObject

Return every asset folder we need to serve from



105
106
107
108
109
110
111
112
113
114
# File 'lib/volt/server/rack/component_paths.rb', line 105

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_paths(name) ⇒ Object

Returns all paths for a specific component



94
95
96
97
98
99
100
101
102
# File 'lib/volt/server/rack/component_paths.rb', line 94

def component_paths(name)
  folders = components[name]

  if folders
    return folders
  else
    return nil
  end
end

#componentsObject

returns an array of every folder that is a component



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/volt/server/rack/component_paths.rb', line 36

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[/[^\/]+$/]

        # Add in the folder if it's not alreay in there
        folders = (@components[folder_name] ||= [])
        unless folders.include?(folder)
          folders << folder
        end
      end
    end
  end

  @components
end

#load_views_and_routes(page) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/volt/server/rack/component_paths.rb', line 76

def load_views_and_routes(page)
  component_names = []
  app_folders do |app_folder|
    Dir["#{app_folder}/*"].map { |cp| cp[/[^\/]+$/] }.each do |component_name|
      component_names << component_name
    end
  end

  # Load in all views and routes
  # TODO: Nested components listed twice are are loaded multiple times
  component_names.uniq.each do |component_name|
    code = Volt::ComponentCode.new(component_name, self, false).code
    # Evaluate returned code, the ```page``` variable is set for access.
    eval(code)
  end
end

#require_in_components(page) ⇒ Object

Makes each components classes available on the load path, require classes.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/server/rack/component_paths.rb', line 58

def require_in_components(page)
  if RUBY_PLATFORM == 'opal'
  else
    app_folders do |app_folder|
      $LOAD_PATH.unshift(app_folder)

      # Sort so we get consistent load order across platforms
      Dir["#{app_folder}/*/{lib,controllers,models,tasks}/*.rb"].each do |ruby_file|
        path = ruby_file.gsub(/^#{app_folder}\//, '')[0..-4]
        require(path)
      end
    end

    # Delay the loading of views
    page.template_loader = -> { load_views_and_routes(page) }
  end
end