Class: Middleman::Apps::Base

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/middleman/apps/base.rb

Overview

Base application class for creating child applications.

Inheriting from this class provides better integration with the static middleman app.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#mm_appObject (readonly)

Middleman Application instance for references to config, sitemap, etc.

Lazily evaluated since this is a bit costly.



23
# File 'lib/middleman/apps/base.rb', line 23

set :mm_app, Middleman::Apps.middleman_app

#staticObject (readonly)

Serve static assets from #public_folder, if found



14
# File 'lib/middleman/apps/base.rb', line 14

set :static, true

#viewsObject (readonly)

Path to the directory containing our layout files.



27
# File 'lib/middleman/apps/base.rb', line 27

set :views, File.join(settings.mm_app.root, 'source', 'layouts')

Class Method Details

.add_routes_to_metadata(*verbs) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/middleman/apps/base.rb', line 65

def self.(*verbs)
  verbs = %i[get post put patch delete] if verbs.empty?
  app_routes = verbs.map do |verb|
    (routes[verb.to_s.upcase] || []).map do |route|
      "##{verb.to_s.upcase} #{route[0]}"
    end
  end.flatten
   :routes, app_routes, overwrite: true
end

.app_resourceObject



45
46
47
# File 'lib/middleman/apps/base.rb', line 45

def self.app_resource
  Middleman::Apps.find_app_resource_for(self, settings.mm_app)
end

.metadataObject



49
50
51
52
53
54
55
56
57
# File 'lib/middleman/apps/base.rb', line 49

def self.
  res  = app_resource
  data = res ? res.locals : {}
  keys = %i[routes html_description]
  data = keys.map { |key| [key, res.send(key)] }.to_h.merge(data) if res
  Hashie::Mash.new(data)
rescue NameError
  data
end

.set_metadata(key, val, overwrite: false) ⇒ Object



59
60
61
62
63
# File 'lib/middleman/apps/base.rb', line 59

def self.(key, val, overwrite: false)
  return if !overwrite && settings.respond_to?("app_#{key}")
  set "app_#{key}", val
  app_resource && app_resource.update_locals(key, val)
end