Class: Munge::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/munge/item.rb

Overview

Items are the core data object for building and manipulating pages.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, relpath:, id:, content: nil, frontmatter: {}, stat: nil) ⇒ Item



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/munge/item.rb', line 4

def initialize(type:,
               relpath:,
               id:,
               content: nil,
               frontmatter: {},
               stat: nil)
  @type = type
  @relpath = relpath
  @id = id
  @content = content
  @frontmatter = Munge::Util::SymbolHash.deep_convert(frontmatter)
  @stat = stat

  @route = nil
  @layout = nil
  @transforms = []
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



23
24
25
# File 'lib/munge/item.rb', line 23

def content
  @content
end

#frontmatterObject (readonly)

Returns the value of attribute frontmatter.



23
24
25
# File 'lib/munge/item.rb', line 23

def frontmatter
  @frontmatter
end

#idObject (readonly)

Returns the value of attribute id.



22
23
24
# File 'lib/munge/item.rb', line 22

def id
  @id
end

#layoutObject

Returns the value of attribute layout.



26
27
28
# File 'lib/munge/item.rb', line 26

def layout
  @layout
end

#relpathObject (readonly)

Returns the value of attribute relpath.



22
23
24
# File 'lib/munge/item.rb', line 22

def relpath
  @relpath
end

#statObject (readonly)

Returns the value of attribute stat.



24
25
26
# File 'lib/munge/item.rb', line 24

def stat
  @stat
end

#transformsObject (readonly)

Returns the value of attribute transforms.



26
27
28
# File 'lib/munge/item.rb', line 26

def transforms
  @transforms
end

Instance Method Details

#[](key) ⇒ Object



57
58
59
# File 'lib/munge/item.rb', line 57

def [](key)
  @frontmatter[key]
end

#[]=(key, value) ⇒ Object



62
63
64
# File 'lib/munge/item.rb', line 62

def []=(key, value)
  @frontmatter[key] = value
end

#basenameString



39
40
41
# File 'lib/munge/item.rb', line 39

def basename
  Munge::Util::Path.basename_no_extension(@relpath)
end

#basepathString



44
45
46
47
48
49
# File 'lib/munge/item.rb', line 44

def basepath
  dirname = Munge::Util::Path.dirname(@relpath)
  basename = Munge::Util::Path.basename_no_extension(@relpath)

  Munge::Util::Path.join(dirname, basename)
end

#binary?true, false



87
88
89
# File 'lib/munge/item.rb', line 87

def binary?
  @type == :binary
end

#dirnameString



29
30
31
# File 'lib/munge/item.rb', line 29

def dirname
  Munge::Util::Path.dirname(@relpath)
end

#extensionsArray<String>



52
53
54
# File 'lib/munge/item.rb', line 52

def extensions
  Munge::Util::Path.extnames(@relpath)
end

#filenameString



34
35
36
# File 'lib/munge/item.rb', line 34

def filename
  Munge::Util::Path.basename(@relpath)
end

#freezevoid

This method returns an undefined value.

Deep freeze. Freezes all instance variables as well as itself.



129
130
131
132
133
# File 'lib/munge/item.rb', line 129

def freeze
  freeze_all_instance_variables

  super
end

#relpath?(*subdir_patterns) ⇒ true, false

Runs a regex match to see if item was found in the specified directory. Do not query with any slashes. Each argument will automatically be joined by slashes. Note though that the string will be converted into a regex.



98
99
100
101
# File 'lib/munge/item.rb', line 98

def relpath?(*subdir_patterns)
  regexp = generate_regex(subdir_patterns)
  Munge::Util::BooleanRegex.match?(regexp, @relpath)
end

#routeString?



73
74
75
76
77
# File 'lib/munge/item.rb', line 73

def route
  if @route
    "/#{@route}"
  end
end

#route=(new_route) ⇒ String



68
69
70
# File 'lib/munge/item.rb', line 68

def route=(new_route)
  @route = remove_surrounding_slashes(new_route)
end

#route?(*subdir_patterns) ⇒ true, false

Runs a regex match to see if the item matches a route. See #relpath?



108
109
110
111
# File 'lib/munge/item.rb', line 108

def route?(*subdir_patterns)
  regexp = generate_regex(subdir_patterns)
  Munge::Util::BooleanRegex.match?(regexp, @route)
end

#text?true, false



81
82
83
# File 'lib/munge/item.rb', line 81

def text?
  @type == :text
end

#transform(engine = :use_extensions) ⇒ void



122
123
124
# File 'lib/munge/item.rb', line 122

def transform(engine = :use_extensions)
  @transforms.push(engine)
end