lib/brief/document.rb in brief-0.0.5 vs lib/brief/document.rb in brief-1.0.0

- old
+ new

@@ -1,68 +1,73 @@ - module Brief class Document - attr_reader :content, :options + include Brief::Document::Rendering + include Brief::Document::FrontMatter - def initialize(options) + attr_accessor :path, :content, :frontmatter + + def initialize(path, options={}) + @path = Pathname(path) @options = options - case - when options.is_a?(String) - @content = options - when options.is_a?(Pathname) - @content = options.read - when options.is_a?(Hash) - @content = options[:path].read + if self.path.exist? + content + load_frontmatter end + + self.model_class.try(:models).try(:<<, to_model) unless model_instance_registered? end - def write using - using.run_before_hooks(:write) do - write - end + def content + @content ||= path.read end - def publish using=nil - using ||= publisher + def extract_content(*args) + options = args.extract_options! + args = options[:args] if options.is_a?(Hash) && options.key?(:args) - document = self - using.run_before_hooks(:publish) do - process(document) + case + when args.length == 1 && args.first.is_a?(String) + css(args.first).try(:text).to_s end end - def publisher - @publisher ||= Brief::Publisher.find(options[:publisher] || "default") + def data + frontmatter end - def method_missing meth, *args, &block - if parser.respond_to?(meth) - return parser.send(meth, *args, &block) - end - - super + def extension + path.extname end - def checksum - @checksum ||= Digest::MD5.hexdigest(content) + def to_model + model_class.new(data.to_hash.merge(path: path, document: self)) if model_class end - def parser - @parser ||= Brief::Parser.new(content, checksum: checksum) + def model_class + @model_class || ((data && data.type) && Brief::Model.for_type(data.type)) end - - def settings - parser.front_matter + # 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 + end end - def elements - parser.send(:elements) + def respond_to?(method) + super || data.respond_to?(method) || data.key?(method) end - def tree - parser.send(:tree) + def method_missing(meth, *args, &block) + if data.respond_to?(meth) + data.send(meth, *args, &block) + else + super + end end end end +