module KVM class Resource def exists? false end def generate! end def get exists = exists? generate! unless exists !exists end def name nil end def self.list [] end end class FileSystemResource < KVM::Resource attr_reader :path def initialize(path) @path = path end def name File.basename(@path) end def exists? !@path.nil? and self.class.is_resource?(@path) end def nested_path(paths) File.expand_path(File.join(paths), path) end def self.list(glob=File.join(self::BASE_DIR, "*")) Dir[glob].select { |f| is_resource?(f) }.map { |f| from_file(f) } end protected def self.is_resource?(file) file.exists?(file) end def self.from_file(file) file end end class FileResource < KVM::FileSystemResource protected def self.is_resource?(f) File.file?(f) end end class DirResource < KVM::FileSystemResource protected def self.is_resource?(f) File.directory?(f) end end end