Class: Ottogen::CollectionItem

Inherits:
Object
  • Object
show all
Defined in:
lib/ottogen/collection_item.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, collection_name:, front_matter:, body:) ⇒ CollectionItem

Returns a new instance of CollectionItem.



20
21
22
23
24
25
# File 'lib/ottogen/collection_item.rb', line 20

def initialize(path:, collection_name:, front_matter:, body:)
  @path = path
  @collection_name = collection_name
  @front_matter = front_matter
  @body = body
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



54
55
56
57
58
59
# File 'lib/ottogen/collection_item.rb', line 54

def method_missing(name, *args)
  key = name.to_s
  return @front_matter[key] if @front_matter.key?(key)

  super
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



17
18
19
# File 'lib/ottogen/collection_item.rb', line 17

def body
  @body
end

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



17
18
19
# File 'lib/ottogen/collection_item.rb', line 17

def collection_name
  @collection_name
end

#front_matterObject (readonly)

Returns the value of attribute front_matter.



17
18
19
# File 'lib/ottogen/collection_item.rb', line 17

def front_matter
  @front_matter
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/ottogen/collection_item.rb', line 17

def path
  @path
end

Returns the value of attribute permalink.



18
19
20
# File 'lib/ottogen/collection_item.rb', line 18

def permalink
  @permalink
end

Class Method Details

.read(path, collection_name) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/ottogen/collection_item.rb', line 9

def self.read(path, collection_name)
  raw = File.read(path)
  front_matter, body = FrontMatter.split(raw, path)
  new(path: path, collection_name: collection_name, front_matter: front_matter, body: body)
rescue FrontMatter::Error => e
  raise Error, e.message
end

Instance Method Details

#asciidoctor_attributesObject



43
44
45
46
47
48
# File 'lib/ottogen/collection_item.rb', line 43

def asciidoctor_attributes
  attrs = @front_matter.transform_keys { |key| "page_#{key}" }
  attrs['page_url'] = url
  attrs['page_slug'] = slug
  attrs
end

#output_path(build_dir) ⇒ Object



37
38
39
40
41
# File 'lib/ottogen/collection_item.rb', line 37

def output_path(build_dir)
  return @permalink.output_path(build_dir) if @permalink

  File.join(build_dir, @collection_name, "#{slug}.html")
end

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/ottogen/collection_item.rb', line 50

def respond_to_missing?(name, include_private = false)
  @front_matter.key?(name.to_s) || super
end

#slugObject



27
28
29
# File 'lib/ottogen/collection_item.rb', line 27

def slug
  File.basename(@path, '.adoc')
end

#urlObject



31
32
33
34
35
# File 'lib/ottogen/collection_item.rb', line 31

def url
  return @permalink.url if @permalink

  "/#{@collection_name}/#{slug}.html"
end