Class: Dimples::Entries::Base
- Inherits:
-
Object
- Object
- Dimples::Entries::Base
- Includes:
- Forwardable
- Defined in:
- lib/dimples/entries/base.rb
Overview
A class representing a dynamic source entry
Constant Summary collapse
- FRONT_MATTER_PATTERN =
/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m
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(site:, source:) ⇒ Base
constructor
A new instance of Base.
- #method_missing(method_name, *_args) ⇒ Object
- #output_directory ⇒ Object
- #parse_metadata(contents) ⇒ Object
- #render(context: {}, body: nil) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #template ⇒ Object
- #write(output_path: nil, context: {}) ⇒ Object
Constructor Details
#initialize(site:, source:) ⇒ Base
Returns a new instance of Base.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dimples/entries/base.rb', line 17 def initialize(site:, source:) @site = site 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
75 76 77 |
# File 'lib/dimples/entries/base.rb', line 75 def method_missing(method_name, *_args) @metadata.send(method_name) end |
Instance Attribute Details
#contents ⇒ Object
Returns the value of attribute contents.
14 15 16 |
# File 'lib/dimples/entries/base.rb', line 14 def contents @contents end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
15 16 17 |
# File 'lib/dimples/entries/base.rb', line 15 def @metadata end |
#path ⇒ Object
Returns the value of attribute path.
14 15 16 |
# File 'lib/dimples/entries/base.rb', line 14 def path @path end |
#rendered_contents ⇒ Object
Returns the value of attribute rendered_contents.
14 15 16 |
# File 'lib/dimples/entries/base.rb', line 14 def rendered_contents @rendered_contents end |
Instance Method Details
#output_directory ⇒ Object
67 68 69 |
# File 'lib/dimples/entries/base.rb', line 67 def output_directory @output_directory ||= @site.config.build_paths[:root] end |
#parse_metadata(contents) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dimples/entries/base.rb', line 31 def (contents) = matches = contents.match(FRONT_MATTER_PATTERN) if matches .merge!(YAML.safe_load(matches[1], symbolize_names: true, permitted_classes: [Date])) @contents = matches.post_match.strip else @contents = contents end @metadata = Metadata.new() end |
#render(context: {}, body: nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dimples/entries/base.rb', line 55 def render(context: {}, body: nil) context[:site] ||= @site. context[:page] ||= @metadata @rendered_contents = template.render(Metadata.new(context)) { body } layout = @site.layouts[@metadata.layout.to_sym] if @metadata.layout return @rendered_contents if layout.nil? layout.render(context: context, body: @rendered_contents) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
79 80 81 |
# File 'lib/dimples/entries/base.rb', line 79 def respond_to_missing?(method_name, include_private = false) true end |
#template ⇒ Object
71 72 73 |
# File 'lib/dimples/entries/base.rb', line 71 def template @template ||= Tilt::ERBTemplate.new { @contents } end |
#write(output_path: nil, context: {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/dimples/entries/base.rb', line 45 def write(output_path: nil, context: {}) output_path = File.join(output_directory, @metadata[:filename]) if output_path.nil? parent_directory = File.dirname(output_path) output = render(context: context) FileUtils.mkdir_p(parent_directory) unless File.directory?(parent_directory) File.write(output_path, output) end |