Class: Middleman::Sitemap::AppCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman/sitemap/app_collection.rb

Overview

Resource manipulator class that handles list of all our child app resources.

To evaluate anything in the context of an instance of this class, use:

Middleman::Apps.with_app_list do
  apps_list # => this will be returned by the block
end

Instance Method Summary collapse

Constructor Details

#initialize(app, _extension, options = {}) ⇒ AppCollection

Returns a new instance of AppCollection.



13
14
15
16
17
18
# File 'lib/middleman/sitemap/app_collection.rb', line 13

def initialize(app, _extension, options = {})
  @app = app
  @options = options
  @sitemap = app.sitemap
  @app_dir = app.root_path.join(options.app_dir)
end

Instance Method Details

#apps_listArray<Middleman::Sitemap::AppResource>

Get a list of all child app resources found in child apps directory.

All child apps will be reloaded everytime this method is called.

Returns:



35
36
37
38
39
40
41
42
# File 'lib/middleman/sitemap/app_collection.rb', line 35

def apps_list
  Dir[@app_dir.join('*.rb').to_s].map do |file|
    path = Pathname.new(file)
    resource = create_app(path) if path.file?
    warn "Ignored child app: #{path}" unless resource
    resource
  end.compact
end

#create_app(path) ⇒ Middleman::Sitemap::AppResource

Create or reload child app resource for each found child app.

Parameters:

  • path (String)

    to the child app source file

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/middleman/sitemap/app_collection.rb', line 49

def create_app(path)
  reload_resource_at path
  url    = get_application_url_for(path)
  klass  = get_application_class_for(path)
  return unless klass

  source = get_source_file(path, @app_dir, :app)
  title  = (klass || url).to_s.titleize
  AppResource.new(@sitemap, url.gsub(%r{^\/}, ''), source).tap do |p|
    p. locals: { url: url, klass: klass, title: title }
  end
end

#manipulate_resource_list(resources) ⇒ Array<Middleman::Sitemap::Resource>

Add our child apps to the list of resources managed by MM.

Parameters:

  • resources (Array<Middleman::Sitemap::Resource>)
    • resource list

Returns:

  • (Array<Middleman::Sitemap::Resource>)

    updated resource list



25
26
27
# File 'lib/middleman/sitemap/app_collection.rb', line 25

def manipulate_resource_list(resources)
  resources + apps_list
end

#middleman_static_appRack::App

Get a Rack::App that can serve the MM app’s build directory.

Directory paths, and 404 error page are deduced from extensions’ options.

Returns:

  • (Rack::App)

    Rack::TryStatic app for MM app’s build directory.



87
88
89
90
91
92
93
# File 'lib/middleman/sitemap/app_collection.rb', line 87

def middleman_static_app
  not_found = @options.not_found
  return create_static_app(build_dir) unless not_found

  not_found_path = File.join(build_dir, find_resource(not_found))
  create_static_app build_dir, not_found_path
end

#mount_child_apps(rack_app = nil) ⇒ Rack::App

Mount all child apps on a specific Rack app (or current app)

Warning is raised (if ‘verbose` option is `true`) when a child app was found, but could not be mapped due to the specified config.

Parameters:

  • rack_app (Rack::App) (defaults to: nil)

    app on which to mount child apps Default: app from MM configuration

Returns:

  • (Rack::App)

    rack_app with child apps mounted on top



72
73
74
75
76
77
78
# File 'lib/middleman/sitemap/app_collection.rb', line 72

def mount_child_apps(rack_app = nil)
  rack_app ||= @app
  apps_list.each do |res|
    rack_app.map(res.url) { run res.klass } if res.klass
  end
  rack_app
end