Sha256: 378a2b542c09632fbe4031b9d431b5512ef6925a75c9a0c2b79d3cc2b86a95bb

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module Analects
  class Source
    include Enumerable
    attr_reader :options

    def initialize(options = {})
      @options = options
    end

    def name      ; options[:name]              ; end
    def url       ; options[:url]               ; end
    def retrieval ; Array(options[:retrieval])  ; end

    def loader
      @loader ||= options[:loader].new(Pathname(location))
    end

    def data_dir
      options[:data_dir]
    end

    def location
      options[:data_file] ? File.join( data_dir, options[:data_file] ) : File.join( data_dir, options[:name].to_s )
    end

    def data_file_present?
      File.exist? location
    end

    def retrieve
      retrieve! unless data_file_present?
    end

    def retrieve!
      retrieval.inject( url ) do | result, method |
        self.send( "retrieve_#{method}", result )
      end
    end

    # url -> stream
    def retrieve_http( url )
      require 'open-uri'
      open( url )
    end

    # gzipped stream -> uncompressed stream
    def retrieve_gunzip( stream )
      require 'zlib'
      Zlib::GzipReader.new( stream )
    end

    # stream|string -> create data file
    def retrieve_save( data )
      File.open( location, 'w' ) do |f|
        f << ( data.respond_to?(:read) ? data.read : data )
      end
    end

    # url -> clones repo
    def retrieve_git( url )
      `git clone #{url} #{data_dir}/#{name}` # Admittedly crude
    end

    def each(&block)
      return to_enum unless block_given?
      loader.each(&block)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
analects-0.2.1 lib/analects/source.rb
analects-0.2.0 lib/analects/source.rb