repubmark/lib/repubmark/elems/article.rb

76 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Repubmark
module Elems
class Article < Base
attr_reader :config
def initialize(config) # rubocop:disable Lint/MissingSuper
@parent = nil
self.config = config
end
#################
# Basic methods #
#################
def word_count
(@annotation&.word_count || 0) +
(@chapter&.word_count || 0)
end
def to_html
[
@annotation&.to_html,
@chapter&.to_html,
].compact.join.freeze
end
def to_gemtext
[
@annotation&.to_gemtext,
@chapter&.to_gemtext,
].compact.join("\n\n\n").freeze
end
###################
# Builder methods #
###################
def annotation
raise 'Annotation already exists' if @annotation
raise 'Annotation after chapter' if @chapter
@annotation = Annotation.new self
yield @annotation
nil
end
def respond_to_missing?(method_name, _include_private)
chapter = @chapter || Chapter.new(self)
chapter.respond_to?(method_name) || super
end
def method_missing(method_name, ...)
chapter = @chapter || Chapter.new(self)
if chapter.respond_to? method_name
@chapter = chapter
@chapter.public_send(method_name, ...)
else
super
end
end
private
def config=(config)
unless config.instance_of? Config
raise TypeError, "Expected #{Config}, got #{config.class}"
end
@config = config
end
end
end
end