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
readonly
Returns the value of attribute contents.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
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.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/dimples/entry.rb', line 14 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
42 43 44 |
# File 'lib/dimples/entry.rb', line 42 def method_missing(method_name, *_args) [method_name.to_sym] end |
Instance Attribute Details
#contents ⇒ Object (readonly)
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.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def @metadata end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/dimples/entry.rb', line 12 def path @path end |
Instance Method Details
#parse_metadata(contents) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dimples/entry.rb', line 26 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
38 39 40 |
# File 'lib/dimples/entry.rb', line 38 def render(context: nil) contents end |
#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean
46 47 48 |
# File 'lib/dimples/entry.rb', line 46 def respond_to_missing?(method_name, _include_private = false) .key?(method_name.to_sym) || super end |