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

Returns a new instance of 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

Parameters:

  • key (String)


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

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

#[]=(key, value) ⇒ Object

Parameters:

  • key (String)


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

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

#basenameString

Returns filename before the first “.”.

Returns:

  • (String)

    filename before the first “.”



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

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

#basepathString

Returns relpath, without extensions.

Returns:

  • (String)

    relpath, without extensions



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

Returns whether or not this item is a binary type (determined by extension).

Returns:

  • (true, false)

    whether or not this item is a binary type (determined by extension)



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

def binary?
  @type == :binary
end

#dirnameString

Returns dirname without leading “.”.

Returns:

  • (String)

    dirname without leading “.”



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

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

#extensionsArray<String>

Returns extensions (everything following the first “.”).

Returns:

  • (Array<String>)

    extensions (everything following the first “.”)



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

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

#filenameString

Returns filename.

Returns:

  • (String)

    filename



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.

Parameters:

  • *subdir_patterns (Array<String>)

Returns:

  • (true, false)

    whether or not this item was found in specified subdirectory



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?

Returns absolute route to this item, if set.

Returns:

  • (String, nil)

    absolute route to this item, if set



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

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

#route=(new_route) ⇒ String

Returns sanitized version of input.

Parameters:

  • new_route (String)

    the compiled route to this item

Returns:

  • (String)

    sanitized version of input



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?

Parameters:

  • *subdir_patterns (Array<String>)

Returns:

  • (true, false)

    whether or not this route belongs to specified subdirectory



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

Returns whether or not this item is a text type (determined by extension).

Returns:

  • (true, false)

    whether or not this item is a text type (determined by extension)



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

def text?
  @type == :text
end

#transform(engine = :use_extensions) ⇒ void

This method returns an undefined value.

Parameters:

  • engine (Symbol) (defaults to: :use_extensions)

    name of template engine to apply onto item when building



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

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