Class: Frontman::App

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



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

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



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/frontman/app.rb', line 144

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.



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

def asset_pipelines
  @asset_pipelines
end

#assets_manifestObject (readonly)

Returns the value of attribute assets_manifest.



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

def assets_manifest
  @assets_manifest
end

#current_pageObject

Returns the value of attribute current_page.



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

def current_page
  @current_page
end

#current_treeObject

Returns the value of attribute current_tree.



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

def current_tree
  @current_tree
end

#data_dirsObject (readonly)

Returns the value of attribute data_dirs.



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

def data_dirs
  @data_dirs
end

#layoutsObject (readonly)

Returns the value of attribute layouts.



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

def layouts
  @layouts
end

#redirectsObject (readonly)

Returns the value of attribute redirects.



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

def redirects
  @redirects
end

#refresh_data_filesObject

Returns the value of attribute refresh_data_files.



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

def refresh_data_files
  @refresh_data_files
end

#view_dataObject

Returns the value of attribute view_data.



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

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



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/frontman/app.rb', line 121

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



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

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



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

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

#appObject



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

def app
  self
end

#get_redirect(url) ⇒ Object



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

def get_redirect(url)
  url = format_url(url)

  @redirects[url]
end

#import_config(file_path) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/frontman/app.rb', line 136

def import_config(file_path)
  if !file_path.end_with?('.rb') && !File.exist?(file_path)
    return import_config("#{file_path}.rb")
  end

  run File.read(file_path)
end

#register_data_dirs(dirs) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/frontman/app.rb', line 99

def register_data_dirs(dirs)
  if dirs.is_a?(Array)
    dirs = dirs.map { |dir| [dir.split('/').last, dir] }.to_h
  end

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

#register_helper_dir(dir) ⇒ Object



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

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



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

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



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

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)


156
157
158
159
160
161
# File 'lib/frontman/app.rb', line 156

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

#run(config) ⇒ Object



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

def run(config)
  instance_eval config
end

#sitemap_treeObject



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

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