lib/brief/document.rb in brief-1.3.2 vs lib/brief/document.rb in brief-1.4.1

- old
+ new

@@ -23,14 +23,36 @@ end model_class.try(:models).try(:<<, to_model) unless model_instance_registered? end + def save + path.open('w') {|fh| fh.write(combined_data_and_content) } + end + + def save! + path.open('w+') {|fh| fh.write(combined_data_and_content) } + end + + def combined_data_and_content + return content if data.nil? || data.empty? + frontmatter.to_hash.to_yaml + "---\n\n#{ content }" + end + def data frontmatter end + def in_briefcase(briefcase) + @briefcase = briefcase + self + end + + def briefcase + @briefcase || Brief.case + end + def sections mappings = model_class.section_mappings @sections = {}.to_mash @@ -40,10 +62,14 @@ end @sections end + def content= value + @content = value + end + def content @content || generate_content end # Shortcut for querying the rendered HTML by css selectors. @@ -86,27 +112,50 @@ 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)) if model_class end + def exist? + path && path.exist? + end + def model_class - @model_class || ((data && data.type) && Brief::Model.for_type(data.type)) + case + when @model_class + @model_class + when data && data.type + Brief::Model.for_type(data.type) + when parent_folder_name.length > 0 + Brief::Model.for_folder_name(parent_folder_name) + else + raise 'Could not determine the model class to use for this document. Specify the type, or put it in a folder that maps to the correct type.' + end end + def document_type + existing = data && data.type + return existing if existing + parent_folder_name.try(:singularize) + end + + def parent_folder_name + path.parent.basename.to_s.downcase + end + # 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? 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) + super || (data && data.respond_to?(method)) || (data && data.key?(method)) end def structure @structure_analyzer ||= Brief::Document::Structure.new(fragment, raw_content.lines.to_a) end