Class: Frontman::App

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Singleton
Defined in:
lib/frontman/app.rb

Defined Under Namespace

Classes: ExistingResourceError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/frontman/app.rb', line 19

def initialize
  @current_page = nil
  @current_tree = nil
  @view_data = []
  @layouts = []
  @redirects = {}
  @assets_manifest = {}
  @data_dirs = {}
  @refresh_data_files = false
  @asset_pipelines = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *_) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/frontman/app.rb', line 130

def method_missing(method_id, *_)
  from_view_data = get_from_view_data(method_id)
  return from_view_data unless from_view_data.nil?

  from_current_page = get_from_current_page(method_id)
  return from_current_page unless from_current_page.nil?

  return true if method_id == :render_toc

  super
end

Instance Attribute Details

#asset_pipelinesObject

Returns the value of attribute asset_pipelines.



14
15
16
# File 'lib/frontman/app.rb', line 14

def asset_pipelines
  @asset_pipelines
end

#assets_manifestObject (readonly)

Returns the value of attribute assets_manifest.



16
17
18
# File 'lib/frontman/app.rb', line 16

def assets_manifest
  @assets_manifest
end

#current_pageObject

Returns the value of attribute current_page.



14
15
16
# File 'lib/frontman/app.rb', line 14

def current_page
  @current_page
end

#current_treeObject

Returns the value of attribute current_tree.



14
15
16
# File 'lib/frontman/app.rb', line 14

def current_tree
  @current_tree
end

#data_dirsObject (readonly)

Returns the value of attribute data_dirs.



16
17
18
# File 'lib/frontman/app.rb', line 16

def data_dirs
  @data_dirs
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



16
17
18
# File 'lib/frontman/app.rb', line 16

def layouts
  @layouts
end

#redirectsObject (readonly)

Returns the value of attribute redirects.



16
17
18
# File 'lib/frontman/app.rb', line 16

def redirects
  @redirects
end

#refresh_data_filesObject

Returns the value of attribute refresh_data_files.



14
15
16
# File 'lib/frontman/app.rb', line 14

def refresh_data_files
  @refresh_data_files
end

#view_dataObject

Returns the value of attribute view_data.



14
15
16
# File 'lib/frontman/app.rb', line 14

def view_data
  @view_data
end

Instance Method Details

#add_asset_pipeline(command:, name: nil, source_dir: nil, timing: :before, mode: :all, delay: 0) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/frontman/app.rb', line 116

def add_asset_pipeline(
  command:, name: nil, source_dir: nil,
  timing: :before, mode: :all, delay: 0
)
  @asset_pipelines.push(
    name: name || command,
    source_dir: source_dir,
    command: command,
    timing: timing == :after ? :after : :before,
    mode: mode,
    delay: delay
  )
end

#add_redirect(from, to) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/frontman/app.rb', line 76

def add_redirect(from, to)
  from = format_url(from)

  resource = sitemap_tree.from_url(from)&.resource
  raise ExistingResourceError.create(from, resource) if resource

  @redirects[from] = to
end

#add_to_manifest(key, value) ⇒ Object



93
94
95
# File 'lib/frontman/app.rb', line 93

def add_to_manifest(key, value)
  @assets_manifest[key] = '/' + value.sub(%r{^/}, '')
end

#appObject



37
38
39
# File 'lib/frontman/app.rb', line 37

def app
  self
end

#get_redirect(url) ⇒ Object



86
87
88
89
90
# File 'lib/frontman/app.rb', line 86

def get_redirect(url)
  url = format_url(url)

  @redirects[url]
end

#register_data_dirs(dirs) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/frontman/app.rb', line 98

def register_data_dirs(dirs)
  dirs.each do |dir|
    define_singleton_method dir.to_sym do
      @data_dirs[dir] ||= DataStore.new(File.join(Dir.pwd, dir))
    end
  end
end

#register_helper_dir(dir) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/frontman/app.rb', line 66

def register_helper_dir(dir)
  Frontman::Config.set(:helpers_dir, dir)
  register_helpers(
    Frontman::Bootstrapper.find_helpers_in(
      File.join('.', dir)
    )
  )
end

#register_helpers(helpers) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/frontman/app.rb', line 55

def register_helpers(helpers)
  helpers.each do |helper|
    require T.must(helper[:path])
    singleton_class.send(
      :include,
      Object.const_get(T.must(helper[:name]).to_sym)
    )
  end
end

#register_layout(glob, layout_name) ⇒ Object



47
48
49
50
51
52
# File 'lib/frontman/app.rb', line 47

def register_layout(glob, layout_name)
  layout_dir = Frontman::Config.get(:layout_dir, fallback: 'views/layouts')
  layout = glob, layout_name.nil? ? nil : File.join(layout_dir, layout_name)

  @layouts.push(layout)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
# File 'lib/frontman/app.rb', line 142

def respond_to_missing?(method_name, include_private = false)
  public_send(method_name)
  true
rescue StandardError
  super
end

#run(config) ⇒ Object



42
43
44
# File 'lib/frontman/app.rb', line 42

def run(config)
  instance_eval config
end

#sitemap_treeObject



32
33
34
# File 'lib/frontman/app.rb', line 32

def sitemap_tree
  @sitemap_tree ||= Frontman::SitemapTree.new(nil)
end