Class: Lookbook::Page

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/lookbook/page.rb

Constant Summary collapse

FRONTMATTER_FIELDS =
[
  :id,
  :label,
  :title,
  :hidden,
  :landing,
  :position,
  :markdown,
  :header,
  :footer,
  :data
]

Constants included from Utils

Utils::FRONTMATTER_REGEX, Utils::POSITION_PREFIX_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, base_path) ⇒ Page

Returns a new instance of Page.



20
21
22
23
24
25
# File 'lib/lookbook/page.rb', line 20

def initialize(path, base_path)
  @pathname = Pathname.new path
  @base_path = base_path
  @options = nil
  @errors = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



84
85
86
87
88
89
90
# File 'lib/lookbook/page.rb', line 84

def method_missing(method_name, *args, &block)
  if args.none? && !block
    options[method_name]
  else
    super
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/lookbook/page.rb', line 18

def errors
  @errors
end

Class Method Details

.allObject



145
146
147
148
149
150
151
152
153
# File 'lib/lookbook/page.rb', line 145

def all
  pages = Array(page_paths).map do |dir|
    Dir["#{dir}/**/*.html.*", "#{dir}/**/*.md.*"].sort.map do |page|
      Lookbook::Page.new(page, dir)
    end
  end
  sorted_pages = pages.flatten.uniq { |p| p.path }.sort_by { |page| [page.position, page.label] }
  PageCollection.new(sorted_pages)
end

.exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/lookbook/page.rb', line 141

def exists?(path)
  !!find(path)
end

.find(path) ⇒ Object



137
138
139
# File 'lib/lookbook/page.rb', line 137

def find(path)
  all.find { |p| p.lookup_path == path }
end

.page_pathsObject



155
156
157
# File 'lib/lookbook/page.rb', line 155

def page_paths
  Lookbook.config.page_paths.filter { |dir| Dir.exist? dir }
end

Instance Method Details

#contentObject



64
65
66
# File 'lib/lookbook/page.rb', line 64

def content
  @content ||= strip_frontmatter(file_contents).strip
end

#footer?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/lookbook/page.rb', line 56

def footer?
  options[:footer] == true
end

#full_pathObject



36
37
38
# File 'lib/lookbook/page.rb', line 36

def full_path
  Rails.root.join(@pathname.to_s)
end

#get(key) ⇒ Object



60
61
62
# File 'lib/lookbook/page.rb', line 60

def get(key)
  options[key]
end

#header?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/lookbook/page.rb', line 52

def header?
  options[:header] == true
end

#hidden?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/lookbook/page.rb', line 44

def hidden?
  options[:hidden] == true
end

#hierarchy_depthObject



72
73
74
# File 'lib/lookbook/page.rb', line 72

def hierarchy_depth
  path.split("/").size
end

#lookup_pathObject



32
33
34
# File 'lib/lookbook/page.rb', line 32

def lookup_path
  @lookup_path ||= to_lookup_path(path)
end

#markdown?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/lookbook/page.rb', line 48

def markdown?
  options[:markdown] == true
end

#matchersObject



68
69
70
# File 'lib/lookbook/page.rb', line 68

def matchers
  normalize_matchers(label)
end

#nameObject



40
41
42
# File 'lib/lookbook/page.rb', line 40

def name
  remove_position_prefix(path_name)
end

#parent_collections_namesObject



76
77
78
# File 'lib/lookbook/page.rb', line 76

def parent_collections_names
  File.dirname(path).split("/")
end

#pathObject



27
28
29
30
# File 'lib/lookbook/page.rb', line 27

def path
  rel_path = @pathname.relative_path_from(@base_path)
  (rel_path.dirname.to_s == "." ? name : "#{rel_path.dirname}/#{name}")
end

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/lookbook/page.rb', line 92

def respond_to_missing?(method_name, include_private = false)
  FRONTMATTER_FIELDS.include? method_name
end

#typeObject



80
81
82
# File 'lib/lookbook/page.rb', line 80

def type
  :page
end