# hx/backend/hobix - Hobix filesystem backend for Hx # # Copyright (c) 2009-2010 MenTaLguY # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require 'yaml' require 'hx' require 'hx/backend/rawfiles' module Hx module Backend class HobixFilter include Hx::Filter def initialize(input, options) @source = Hx::StripPath.new(input, :suffix => ".yaml") end def yaml_repr(value) YAML.parse(YAML.dump(value)) end private :yaml_repr def edit_entry(path, prototype=nil) if prototype prototype = prototype.dup prototype['content'] = (prototype['content'] || "").dup content = prototype['content'] def content.to_yaml_style ; :literal ; end native = YAML::DomainType.new('hobix.com,2004', 'entry', prototype) prototype = { 'content' => YAML.dump(native) } end @source.edit_entry(path, prototype) do |text| begin previous_mtime = @source.get_entry(path)['updated'] rescue Hx::NoSuchEntryError previous_mtime = nil end text = yield text repr = YAML.parse(text) keys = {} repr.value.each_key { |key| keys[key.value] = key } %w(created updated).each { |name| keys[name] ||= yaml_repr(name) } update_time = Time.now update_time_repr = yaml_repr(update_time) previous_mtime ||= update_time previous_mtime_repr = yaml_repr(previous_mtime) repr.add(keys['created'], previous_mtime_repr) unless repr['created'] repr.add(keys['updated'], update_time_repr) repr.emit end self end def each_entry_path(selector) @source.each_entry_path(selector) { |path| yield path } self end def get_entry(path) raw_entry = @source.get_entry(path) entry = YAML.load(raw_entry['content'].to_s).value entry['updated'] ||= raw_entry['updated'] entry['created'] ||= raw_entry['created'] || raw_entry['updated'] entry end end class Hobix < HobixFilter def initialize(input, options) files = Hx::Backend::RawFiles.new(input, options) super(files, options) end end end end