lib/brief/document.rb in brief-1.6.1 vs lib/brief/document.rb in brief-1.7.0

- old
+ new

@@ -8,10 +8,22 @@ def document self end + def to_s + "#{ model_class }.at_path(#{relative_path})" + end + + def inspect + "#{ model_class }.at_path(#{relative_path})" + end + + def relative_path + briefcase.present? ? path.relative_path_from(briefcase.docs_path) : path + end + def initialize(path, options = {}) if path.respond_to?(:key?) && options.empty? @frontmatter = path.to_mash else @path = Pathname(path) @@ -25,10 +37,22 @@ elsif options[:contents] @raw_content = options[:contents] end end + def content_hash + Digest::MD5.hexdigest(@content.to_s) + end + + def file_hash + Digest::MD5.hexdigest(path.read.to_s) + end + + def content_stale? + content_hash != file_hash + end + def raw= val @raw_set = true @raw_content = val #document.load_frontmatter @raw_content @@ -53,10 +77,11 @@ @content = nil @raw_content = path.read @frontmatter = nil @raw_frontmatter = nil @refreshing = true + @content_hash = nil load_frontmatter true end def save! @@ -104,13 +129,14 @@ def content= value @content = value end def content - if @content.nil? - generate_content + if @content.nil? && path && path.exist? + @content = path.read end + @content || generate_content end # Shortcut for querying the rendered HTML by css selectors. # @@ -151,11 +177,17 @@ def extension path.extname end + def model_attributes + (data || {}).to_hash + .merge(path: path, document: self) + .reverse_merge(type: document_type) + end + def to_model - model_class.new((data || {}).to_hash.merge(path: path, document: self).reverse_merge(:type=>document_type)) if model_class + model_class.try(:new, model_attributes) end def exist? path && path.exist? end