From bb110bb05a1c6f01d5531cca2081fc31d373a836 Mon Sep 17 00:00:00 2001 From: Jakub Stastny aka botanicus Date: Tue, 16 Aug 2011 11:11:50 -0700 Subject: [PATCH] Fixed metadata handling. --- bin/ace | 1 - lib/ace.rb | 28 ++++++++++++---------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/bin/ace b/bin/ace index 52a2228..7c080f9 100755 --- a/bin/ace +++ b/bin/ace @@ -43,7 +43,6 @@ rules.rules.each do |klass, files| if File.binread(file).match(/^-{3,5}\s*$/) # TODO: this should be a filter or lazy-loaded puts "~ Read #{file} with parse" raw_item = Ace::RawItem.new(file).tap(&:parse) - raw_item.check_metadata_created_at(file) item = klass.create(raw_item.metadata, raw_item.content) else puts "~ Read #{file} without parse" diff --git a/lib/ace.rb b/lib/ace.rb index dbb585e..a506ca8 100644 --- a/lib/ace.rb +++ b/lib/ace.rb @@ -21,28 +21,24 @@ module Ace class RawItem attr_accessor :path, :metadata, :content def initialize(path) + @path = path @data = File.read(path) end - def check_metadata_created_at(path) - if self.metadata[:title] - year, month, day = File.basename(path).slice(0,10).split('-') - self.metadata[:created_at] ||= Date.new(year.to_i, month.to_i, day.to_i) - end - end - def parse pieces = @data.split(/^-{3,5}\s*$/) - # if pieces.size < 3 - # raise RuntimeError.new( - # "The file '#{path}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." - # ) - # end + if pieces.length == 1 || @data.empty? + self.metadata = Hash.new + self.content = pieces.first + else + self.metadata = begin + YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new + end + self.content = pieces[2..-1].join.strip + end - # Parse - self.metadata = YAML.load(pieces[1]).inject(Hash.new) { |metadata, pair| metadata.merge(pair[0].to_sym => pair[1]) } || Hash.new - # TODO: check metadata[:created_at] and supply it from filename - self.content = pieces[2..-1].join.strip + self.metadata[:created_at] ||= File.ctime(self.path) + self.metadata[:updated_at] ||= File.mtime(self.path) end end