Sha256: 3c915605ba429ecd0f6a219ddf34019e5474921d7541a2994c2b0f77f96ca4be

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kvm-0.0.1.pre lib/kvm/resource.rb