Class: Munge::Item
- Inherits:
-
Object
- Object
- Munge::Item
- Defined in:
- lib/munge/item.rb
Overview
Items are the core data object for building and manipulating pages.
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#frontmatter ⇒ Object
readonly
Returns the value of attribute frontmatter.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#relpath ⇒ Object
readonly
Returns the value of attribute relpath.
-
#stat ⇒ Object
readonly
Returns the value of attribute stat.
-
#transforms ⇒ Object
readonly
Returns the value of attribute transforms.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#basename ⇒ String
Filename before the first “.”.
-
#basepath ⇒ String
Relpath, without extensions.
-
#binary? ⇒ true, false
Whether or not this item is a binary type (determined by extension).
-
#dirname ⇒ String
Dirname without leading “.”.
-
#extensions ⇒ Array<String>
Extensions (everything following the first “.”).
-
#filename ⇒ String
Filename.
-
#freeze ⇒ void
Deep freeze.
-
#initialize(type:, relpath:, id:, content: nil, frontmatter: {}, stat: nil) ⇒ Item
constructor
A new instance of Item.
-
#relpath?(*subdir_patterns) ⇒ true, false
Runs a regex match to see if item was found in the specified directory.
-
#route ⇒ String?
Absolute route to this item, if set.
-
#route=(new_route) ⇒ String
Sanitized version of input.
-
#route?(*subdir_patterns) ⇒ true, false
Runs a regex match to see if the item matches a route.
-
#text? ⇒ true, false
Whether or not this item is a text type (determined by extension).
- #transform(engine = :use_extensions) ⇒ void
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
23 24 25 |
# File 'lib/munge/item.rb', line 23 def content @content end |
#frontmatter ⇒ Object (readonly)
Returns the value of attribute frontmatter.
23 24 25 |
# File 'lib/munge/item.rb', line 23 def frontmatter @frontmatter end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
22 23 24 |
# File 'lib/munge/item.rb', line 22 def id @id end |
#layout ⇒ Object
Returns the value of attribute layout.
26 27 28 |
# File 'lib/munge/item.rb', line 26 def layout @layout end |
#relpath ⇒ Object (readonly)
Returns the value of attribute relpath.
22 23 24 |
# File 'lib/munge/item.rb', line 22 def relpath @relpath end |
#stat ⇒ Object (readonly)
Returns the value of attribute stat.
24 25 26 |
# File 'lib/munge/item.rb', line 24 def stat @stat end |
#transforms ⇒ Object (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 |
#basename ⇒ String
Returns filename before the first “.”.
39 40 41 |
# File 'lib/munge/item.rb', line 39 def basename Munge::Util::Path.basename_no_extension(@relpath) end |
#basepath ⇒ String
Returns 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).
87 88 89 |
# File 'lib/munge/item.rb', line 87 def binary? @type == :binary end |
#dirname ⇒ String
Returns dirname without leading “.”.
29 30 31 |
# File 'lib/munge/item.rb', line 29 def dirname Munge::Util::Path.dirname(@relpath) end |
#extensions ⇒ Array<String>
Returns extensions (everything following the first “.”).
52 53 54 |
# File 'lib/munge/item.rb', line 52 def extensions Munge::Util::Path.extnames(@relpath) end |
#filename ⇒ String
Returns filename.
34 35 36 |
# File 'lib/munge/item.rb', line 34 def filename Munge::Util::Path.basename(@relpath) end |
#freeze ⇒ void
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 |
#route ⇒ String?
Returns 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.
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
Returns 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.
122 123 124 |
# File 'lib/munge/item.rb', line 122 def transform(engine = :use_extensions) @transforms.push(engine) end |