# 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 'pathname' require 'hx' module Hx module Backend class RawFiles include Hx::Filter def initialize(input, options) @entry_dir = Hx.get_pathname(options, :entry_dir) end def path_to_pathname(path) ; @entry_dir + path ; end private :path_to_pathname def pathname_to_path(pathname) pathname.relative_path_from(@entry_dir).to_s end private :pathname_to_path def edit_entry(path, prototype=nil) pathname = path_to_pathname(path) begin text = pathname.read rescue Errno::ENOENT raise NoSuchEntryError, path unless prototype text = prototype['content'].to_s end text = yield text Hx.write_file(pathname, text) self end def each_entry_path(selector) Pathname.glob(@entry_dir + '**/*') do |pathname| next unless pathname.file? path = pathname_to_path(pathname) yield path if selector.accept? path end self end def get_entry(path) pathname = path_to_pathname(path) begin { 'updated' => pathname.mtime, 'content' => Hx::LazyContent.new { pathname.read } } rescue Errno::ENOENT raise Hx::NoSuchEntryError, path end end end end end