Class: Analects::Source
- Inherits:
-
Object
- Object
- Analects::Source
- Includes:
- Enumerable
- Defined in:
- lib/analects/source.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #data_dir ⇒ Object
- #data_file_present? ⇒ Boolean
- #each(&block) ⇒ Object
-
#initialize(options = {}) ⇒ Source
constructor
A new instance of Source.
- #library ⇒ Object
- #loader ⇒ Object
- #location ⇒ Object
- #name ⇒ Object
- #retrieval ⇒ Object
- #retrieve ⇒ Object
- #retrieve! ⇒ Object
-
#retrieve_git(url) ⇒ Object
url -> clones repo.
-
#retrieve_gunzip(stream) ⇒ Object
gzipped stream -> uncompressed stream.
-
#retrieve_http(url) ⇒ Object
url -> stream.
-
#retrieve_save(data) ⇒ Object
stream|string -> create data file.
- #retrieve_unzip(stream) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Source
Returns a new instance of Source.
6 7 8 |
# File 'lib/analects/source.rb', line 6 def initialize( = {}) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/analects/source.rb', line 4 def @options end |
Instance Method Details
#data_dir ⇒ Object
19 20 21 |
# File 'lib/analects/source.rb', line 19 def data_dir Pathname([:data_dir]) end |
#data_file_present? ⇒ Boolean
29 30 31 |
# File 'lib/analects/source.rb', line 29 def data_file_present? location.exist? end |
#each(&block) ⇒ Object
80 81 82 83 |
# File 'lib/analects/source.rb', line 80 def each(&block) return to_enum unless block_given? loader.each(&block) end |
#library ⇒ Object
10 |
# File 'lib/analects/source.rb', line 10 def library ; [:library] ; end |
#loader ⇒ Object
15 16 17 |
# File 'lib/analects/source.rb', line 15 def loader @loader ||= [:loader].new(Pathname(location), library) end |
#location ⇒ Object
23 24 25 26 27 |
# File 'lib/analects/source.rb', line 23 def location [:data_file] ? data_dir.join([:data_file]) : data_dir.join([:name].to_s) end |
#name ⇒ Object
11 |
# File 'lib/analects/source.rb', line 11 def name ; [:name] ; end |
#retrieval ⇒ Object
13 |
# File 'lib/analects/source.rb', line 13 def retrieval ; Array([:retrieval]) ; end |
#retrieve ⇒ Object
33 34 35 |
# File 'lib/analects/source.rb', line 33 def retrieve retrieve! unless data_file_present? end |
#retrieve! ⇒ Object
37 38 39 40 41 |
# File 'lib/analects/source.rb', line 37 def retrieve! retrieval.inject(url) do | result, method | self.send( "retrieve_#{method}", result ) end end |
#retrieve_git(url) ⇒ Object
url -> clones repo
76 77 78 |
# File 'lib/analects/source.rb', line 76 def retrieve_git(url) `git clone #{url} #{data_dir}/#{name}` # Admittedly crude end |
#retrieve_gunzip(stream) ⇒ Object
gzipped stream -> uncompressed stream
50 51 52 53 |
# File 'lib/analects/source.rb', line 50 def retrieve_gunzip(stream) require 'zlib' Zlib::GzipReader.new(stream) end |
#retrieve_http(url) ⇒ Object
url -> stream
44 45 46 47 |
# File 'lib/analects/source.rb', line 44 def retrieve_http(url) require 'open-uri' StringIO.new(open(url).read) end |
#retrieve_save(data) ⇒ Object
stream|string -> create data file
69 70 71 72 73 |
# File 'lib/analects/source.rb', line 69 def retrieve_save(data) File.open( location, 'w' ) do |f| f << ( data.respond_to?(:read) ? data.read : data ) end end |
#retrieve_unzip(stream) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/analects/source.rb', line 55 def retrieve_unzip(stream) require 'zip' location.mkdir unless location.exist? Zip::InputStream.open(stream) do |io| while (entry = io.get_next_entry) next if entry.ftype == :symlink loc = location.join(entry.name) loc.delete if loc.exist? entry.extract(loc) end end end |
#url ⇒ Object
12 |
# File 'lib/analects/source.rb', line 12 def url ; [:url] ; end |