Class: Antoinette::LayoutResolver
- Inherits:
-
Object
- Object
- Antoinette::LayoutResolver
- Defined in:
- lib/antoinette/services/layout_resolver.rb
Instance Method Summary collapse
- #apps_for(template_path) ⇒ Object
-
#initialize(layout_dirs: ["app/views/layouts"], default_layout: "application") ⇒ LayoutResolver
constructor
A new instance of LayoutResolver.
- #layout_apps_map ⇒ Object
- #layout_for(template_path) ⇒ Object
Constructor Details
#initialize(layout_dirs: ["app/views/layouts"], default_layout: "application") ⇒ LayoutResolver
Returns a new instance of LayoutResolver.
7 8 9 10 |
# File 'lib/antoinette/services/layout_resolver.rb', line 7 def initialize(layout_dirs: ["app/views/layouts"], default_layout: "application") @layout_dirs = layout_dirs @default_layout = default_layout end |
Instance Method Details
#apps_for(template_path) ⇒ Object
42 43 44 45 |
# File 'lib/antoinette/services/layout_resolver.rb', line 42 def apps_for(template_path) layout = layout_for(template_path) layout_apps_map[layout] || [] end |
#layout_apps_map ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/antoinette/services/layout_resolver.rb', line 12 def layout_apps_map @layout_apps_map ||= @layout_dirs.each_with_object({}) do |dir, result| Dir.glob(Rails.root.join(dir, "*.html.erb")).each do |file_path| content = File.read(file_path) apps = content.scan(/Elm\.(\w+)\.init/).flatten.uniq name = File.basename(file_path, ".html.erb") result[name] = ((result[name] || []) + apps).uniq end end end |
#layout_for(template_path) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/antoinette/services/layout_resolver.rb', line 23 def layout_for(template_path) full_path = if Pathname.new(template_path).absolute? template_path else Rails.root.join(template_path).to_s end return @default_layout unless File.exist?(full_path) content = File.read(full_path) return @default_layout unless content.start_with?("---") frontmatter = content.match(/\A---\s*\n(.*?\n)---/m) return @default_layout unless frontmatter yaml = YAML.safe_load(frontmatter[1]) yaml&.fetch("layout", @default_layout) || @default_layout end |