Class: Frontmatter::Page

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
app/models/frontmatter/page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Page

Returns a new instance of Page.



20
21
22
23
# File 'app/models/frontmatter/page.rb', line 20

def initialize(filename)
  @slug = File.basename(filename.split('.')[0] )
  @frontmatter = YAML.load(File.read(filename)).with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

TODO: Define attribute methods to speed this up



26
27
28
# File 'app/models/frontmatter/page.rb', line 26

def method_missing(m, *args, &block)
  @frontmatter[m] || super
end

Instance Attribute Details

#slugObject (readonly)

Returns the value of attribute slug.



4
5
6
# File 'app/models/frontmatter/page.rb', line 4

def slug
  @slug
end

Class Method Details

.allObject



6
7
8
9
10
# File 'app/models/frontmatter/page.rb', line 6

def self.all
  Dir[pages_directory]
    .reject { %w(index show).include? File.basename(_1.split('.')[0]) }
    .map { |f| new(f) }
end

.find(slug) ⇒ Object



12
13
14
# File 'app/models/frontmatter/page.rb', line 12

def self.find(slug)
  all.find { _1.slug == slug }
end

.pages_directoryObject



16
17
18
# File 'app/models/frontmatter/page.rb', line 16

def self.pages_directory
  Rails.root.join("app", "views", view_key, '*.yaml').freeze
end

.view_keyObject



38
39
40
# File 'app/models/frontmatter/page.rb', line 38

def self.view_key
  model_name.route_key
end

Instance Method Details

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

Returns:

  • (Boolean)


30
31
32
# File 'app/models/frontmatter/page.rb', line 30

def respond_to_missing?(method_name, include_private = false)
  @frontmatter.key?(method_name) || super
end

#to_paramObject



34
35
36
# File 'app/models/frontmatter/page.rb', line 34

def to_param
  slug
end