Module: IMW::Resources
- Defined in:
- lib/imw/resources.rb,
lib/imw/resources/local.rb,
lib/imw/resources/remote.rb,
lib/imw/resources/archive.rb,
lib/imw/resources/formats.rb,
lib/imw/resources/schemes.rb,
lib/imw/resources/schemes/s3.rb,
lib/imw/resources/compressible.rb,
lib/imw/resources/formats/json.rb,
lib/imw/resources/formats/sgml.rb,
lib/imw/resources/formats/yaml.rb,
lib/imw/resources/schemes/hdfs.rb,
lib/imw/resources/schemes/http.rb,
lib/imw/resources/compressed_file.rb,
lib/imw/resources/formats/delimited.rb,
lib/imw/resources/archives_and_compressed.rb,
lib/imw/resources/archives_and_compressed/gz.rb,
lib/imw/resources/archives_and_compressed/bz2.rb,
lib/imw/resources/archives_and_compressed/rar.rb,
lib/imw/resources/archives_and_compressed/tar.rb,
lib/imw/resources/archives_and_compressed/zip.rb,
lib/imw/resources/archives_and_compressed/targz.rb,
lib/imw/resources/archives_and_compressed/tarbz2.rb
Overview
IMW::Resources is a namespace in which all the modules which define different kinds of behavior for IMW::Resource objects are defined.
When an IMW::Resource is instantiated it eventually calls IMW::Resources#extend_resource! which will iterate through the handlers in IMW::Resources#handlers, extending the resource with modules whose handler conditions are satisfied.
A handler is just an Array with two elements. The first should be a module or a string identifying a module.
If the second element is a Regexp, the corresponding module will be used if the regexp matches the resource’s URI (as a string)
If the second element is a Proc, it will be called with the resource as its only argument and if it returns true then the module will be used.
You can define your own handlers by appending them to IMW::Resources::USER_DEFINED_HANDLERS in your .imwrc
file.
Defined Under Namespace
Modules: Archive, Archives, CompressedFile, CompressedFiles, Compressible, Formats, LocalDirectory, LocalFile, LocalObj, RemoteDirectory, RemoteFile, RemoteObj, Schemes
Constant Summary collapse
- BASIC_HANDLERS =
Basic handlers to determine whether the resource is local, remote, or a string.
[ ["LocalObj", Proc.new { |resource| resource.scheme == 'file' || resource.scheme.blank? } ], ["RemoteObj", Proc.new { |resource| resource.scheme != 'file' && resource.scheme.present? } ], ["StringObj", Proc.new { |resource| resource.is_stringio? } ] ]
- USER_DEFINED_HANDLERS =
Define this constant in your configuration file to add your own handlers.
[]
- ARCHIVE_AND_COMPRESSED_HANDLERS =
Handlers which augment the resource with methods for archiving, extracting, compressing, decompressing…
[ # try compressible first -- compressed files below will override it ["Compressible", Proc.new { |r| r.is_local? } ], # order is important! -- tar.bz2 must come before .bz2, &c. ["Archives::Tarbz2", Proc.new { |r| r.is_local? && r.path =~ /\.tar\.bz2$/ } ], ["Archives::Tarbz2", Proc.new { |r| r.is_local? && r.path =~ /\.tbz2$/ } ], ["CompressedFiles::Bz2", Proc.new { |r| r.is_local? && r.path =~ /\.bz2$/ && r.path !~ /\.tar\.bz2$/ && r.path !~ /\.tbz2$/ } ], ["Archives::Targz", Proc.new { |r| r.is_local? && r.path =~ /\.tar\.gz$/ } ], ["Archives::Targz", Proc.new { |r| r.is_local? && r.path =~ /\.tgz$/ } ], ["CompressedFiles::Gz", Proc.new { |r| r.is_local? && r.path =~ /\.gz$/ && r.path !~ /\.tar\.gz$/ && r.path !~ /\.tgz$/ } ], ["Archives::Tar", Proc.new { |r| r.is_local? && r.path =~ /\.tar$/ } ], ["Archives::Rar", Proc.new { |r| r.is_local? && r.path =~ /\.rar$/ } ], ["Archives::Zip", Proc.new { |r| r.is_local? && r.path =~ /\.zip$/ } ] ]
Constants included from Schemes
Constants included from Formats
Class Method Summary collapse
-
.extend_resource!(resource) ⇒ IMW::Resource
Iterate through IMW::Resources#handlers and extend the given
resource
with modules whose handler conditions match the resource. -
.handlers ⇒ Array
A list of handlers to try.
Class Method Details
.extend_resource!(resource) ⇒ IMW::Resource
Iterate through IMW::Resources#handlers and extend the given resource
with modules whose handler conditions match the resource.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/imw/resources.rb', line 42 def self.extend_resource! resource handlers.each do |mod_name, handler| case handler when Regexp then extend_resource_with_mod_or_string!(resource, mod_name) if handler =~ resource.uri.to_s when Proc then extend_resource_with_mod_or_string!(resource, mod_name) if handler.call(resource) when TrueClass then extend_resource_with_mod_or_string!(resource, mod_name) else raise IMW::TypeError("A handler must be Regexp, Proc, or true") end end resource end |
.handlers ⇒ Array
A list of handlers to try. Define your own handlers in IMW::Resources::USER_DEFINED_HANDLERS.
75 76 77 78 |
# File 'lib/imw/resources.rb', line 75 def self.handlers # order here is important BASIC_HANDLERS + SCHEME_HANDLERS + ARCHIVE_AND_COMPRESSED_HANDLERS + FORMAT_HANDLERS + USER_DEFINED_HANDLERS end |