Class: Dimples::Entry
- Inherits:
-
Object
- Object
- Dimples::Entry
- Defined in:
- lib/dimples/entry.rb
Overview
A class representing a dynamic source entry that can generate rendered content
Constant Summary collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m- DEFAULT_FILENAME =
'index.html'
Instance Attribute Summary collapse
-
#contents ⇒ Object
Returns the value of attribute contents.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ Object
Returns the value of attribute path.
-
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
Instance Method Summary collapse
-
#initialize(source:) ⇒ Entry
constructor
A new instance of Entry.
- #method_missing(method_name, *_args) ⇒ Object
- #parse_metadata(contents) ⇒ Object
- #render(context: nil) ⇒ Object
- #respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
Constructor Details
#initialize(source:) ⇒ Entry
Returns a new instance of Entry.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dimples/entry.rb', line 15 def initialize(source:) contents = case source when Pathname @path = File.(source) File.read(@path) when String source end (contents) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
43 44 45 |
# File 'lib/dimples/entry.rb', line 43 def method_missing(method_name, *_args) [method_name.to_sym] end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def contents @contents end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
13 14 15 |
# File 'lib/dimples/entry.rb', line 13 def @metadata end |
#path ⇒ Object
Returns the value of attribute path.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def path @path end |
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def rendered_contents @rendered_contents end |
Instance Method Details
#parse_metadata(contents) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dimples/entry.rb', line 27 def (contents) @metadata = matches = contents.match(FRONT_MATTER_PATTERN) if matches @metadata.merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date])) @contents = matches.post_match.strip else @contents = contents end end |
#render(context: nil) ⇒ Object
39 40 41 |
# File 'lib/dimples/entry.rb', line 39 def render(context: nil) contents end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
47 48 49 |
# File 'lib/dimples/entry.rb', line 47 def respond_to_missing?(method_name, _include_private = false) .key?(method_name.to_sym) || super end |