lib/brief/document.rb in brief-1.2.0 vs lib/brief/document.rb in brief-1.3.0
- old
+ new
@@ -1,22 +1,30 @@
module Brief
class Document
include Brief::Document::Rendering
include Brief::Document::FrontMatter
+ include Brief::Document::Templating
attr_accessor :path, :content, :frontmatter, :raw_content
- def initialize(path, options={})
- @path = Pathname(path)
- @options = options
+ def initialize(path, options = {})
+ if path.respond_to?(:key?) && options.empty?
+ @frontmatter = path.to_mash
+ else
+ @path = Pathname(path)
+ end
- if self.path.exist?
+ @options = options.to_mash
+
+ if @path && self.path.exist?
@raw_content = path.read
load_frontmatter
+ elsif options[:contents]
+ @raw_content = options[:contents]
end
- self.model_class.try(:models).try(:<<, to_model) unless model_instance_registered?
+ model_class.try(:models).try(:<<, to_model) unless model_instance_registered?
end
def data
frontmatter
end
@@ -32,10 +40,14 @@
end
@sections
end
+ def content
+ @content || generate_content
+ end
+
# Shortcut for querying the rendered HTML by css selectors.
#
# This will allow for model data attributes to be pulled from the
# document contents.
#
@@ -85,21 +97,21 @@
# Each model class tracks the instances of the models created
# and ensures that there is a 1-1 relationship between a document path
# and the model.
def model_instance_registered?
- self.model_class && self.model_class.models.any? do |model|
- model.path == self.path
+ model_class && model_class.models.any? do |model|
+ model.path == path
end
end
def respond_to?(method)
super || data.respond_to?(method) || data.key?(method)
end
def structure
- @structure_analyzer ||= Brief::Document::Structure.new(fragment, self.raw_content.lines.to_a)
+ @structure_analyzer ||= Brief::Document::Structure.new(fragment, raw_content.lines.to_a)
end
def parser
@parser ||= begin
structure.prescan
@@ -118,6 +130,5 @@
super
end
end
end
end
-