lib/brief/document.rb in brief-1.4.4 vs lib/brief/document.rb in brief-1.5.0
- old
+ new
@@ -4,35 +4,70 @@
include Brief::Document::FrontMatter
include Brief::Document::Templating
attr_accessor :path, :content, :frontmatter, :raw_content
+ def document
+ self
+ end
+
def initialize(path, options = {})
if path.respond_to?(:key?) && options.empty?
@frontmatter = path.to_mash
else
@path = Pathname(path)
end
+ if options[:Test]
+ binding.pry
+ end
+
@options = options.to_mash
if @path && self.path.exist?
- @raw_content = path.read
+ @raw_content = self.path.read
load_frontmatter
elsif options[:contents]
@raw_content = options[:contents]
end
+ register_model_instance if self.path && self.path.exist?
+ end
+
+ def register_model_instance
model_class.try(:models).try(:<<, to_model) unless model_instance_registered?
end
+ def raw= val
+ @raw_set = true
+ @raw_content = val
+ #document.load_frontmatter
+ @raw_content
+ end
+
+ def set_raw?
+ !!@raw_set
+ end
+
def save
- path.open('w') {|fh| fh.write(combined_data_and_content) }
+ if set_raw?
+ file_contents = raw_content
+ else
+ file_contents = combined_data_and_content
+ end
+
+ path.open('w') {|fh| fh.write(file_contents) }
end
def save!
- path.open('w+') {|fh| fh.write(combined_data_and_content) }
+ if set_raw?
+ file_contents = raw_content
+ else
+ file_contents = combined_data_and_content
+ end
+
+ path.open('w+') {|fh| fh.write(file_contents) }
end
def combined_data_and_content
return content if data.nil? || data.empty?
frontmatter.to_hash.to_yaml + "---\n\n#{ content }"
@@ -41,16 +76,16 @@
def data
frontmatter
end
def in_briefcase(briefcase)
- @briefcase = briefcase
+ @briefcase_root = briefcase.root
self
end
def briefcase
- @briefcase || Brief.case
+ (@briefcase_root && Brief.cases[@briefcase_root]) || Brief.case
end
def sections
mappings = model_class.section_mappings
@@ -67,10 +102,13 @@
def content= value
@content = value
end
def content
+ if @content.nil?
+ generate_content
+ end
@content || generate_content
end
# Shortcut for querying the rendered HTML by css selectors.
#
@@ -112,11 +150,11 @@
def extension
path.extname
end
def to_model
- model_class.new((data || {}).to_hash.merge(path: path, document: self)) if model_class
+ model_class.new((data || {}).to_hash.merge(path: path, document: self).reverse_merge(:type=>document_type)) if model_class
end
def exist?
path && path.exist?
end
@@ -168,9 +206,13 @@
end
end
def fragment
@fragment ||= Nokogiri::HTML.fragment(to_raw_html)
+ end
+
+ def type
+ document_type
end
def method_missing(meth, *args, &block)
if data.respond_to?(meth)
data.send(meth, *args, &block)