imw 0.2.18 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -26
- data/Gemfile.lock +13 -38
- data/{LICENSE → LICENSE.txt} +1 -1
- data/README.textile +35 -0
- data/Rakefile +45 -22
- data/VERSION +1 -1
- data/examples/foo.rb +19 -0
- data/examples/html_selector.rb +22 -0
- data/examples/nes_game_list.csv +625 -0
- data/examples/nes_gamespot.csv +1371 -0
- data/examples/nes_nintendo.csv +624 -0
- data/examples/nes_unlicensed.csv +89 -0
- data/examples/nes_wikipedia.csv +710 -0
- data/examples/nibbler_test.rb +24 -0
- data/examples/script.rb +19 -0
- data/lib/imw.rb +28 -140
- data/lib/imw/error.rb +9 -0
- data/lib/imw/recordizer.rb +8 -0
- data/lib/imw/recordizer/html_selector_recordizer.rb +86 -0
- data/lib/imw/recordizer/string_slice_recordizer.rb +39 -0
- data/lib/imw/resource.rb +3 -119
- data/lib/imw/serializer.rb +7 -0
- data/lib/imw/serializer/json_serializer.rb +17 -0
- data/lib/imw/uri.rb +41 -0
- data/spec/resource_spec.rb +78 -0
- data/spec/uri_spec.rb +55 -0
- metadata +81 -232
- data/README.rdoc +0 -371
- data/bin/imw +0 -5
- data/bin/tsv_to_json.rb +0 -29
- data/etc/imwrc.rb +0 -26
- data/examples/dataset.rb +0 -12
- data/examples/metadata.yml +0 -10
- data/lib/imw/archives.rb +0 -120
- data/lib/imw/archives/rar.rb +0 -19
- data/lib/imw/archives/tar.rb +0 -19
- data/lib/imw/archives/tarbz2.rb +0 -73
- data/lib/imw/archives/targz.rb +0 -73
- data/lib/imw/archives/zip.rb +0 -51
- data/lib/imw/boot.rb +0 -87
- data/lib/imw/compressed_files.rb +0 -94
- data/lib/imw/compressed_files/bz2.rb +0 -16
- data/lib/imw/compressed_files/compressible.rb +0 -75
- data/lib/imw/compressed_files/gz.rb +0 -16
- data/lib/imw/dataset.rb +0 -125
- data/lib/imw/dataset/paths.rb +0 -29
- data/lib/imw/dataset/workflow.rb +0 -195
- data/lib/imw/formats.rb +0 -33
- data/lib/imw/formats/delimited.rb +0 -170
- data/lib/imw/formats/excel.rb +0 -100
- data/lib/imw/formats/json.rb +0 -41
- data/lib/imw/formats/pdf.rb +0 -71
- data/lib/imw/formats/sgml.rb +0 -69
- data/lib/imw/formats/yaml.rb +0 -41
- data/lib/imw/metadata.rb +0 -83
- data/lib/imw/metadata/contains_metadata.rb +0 -54
- data/lib/imw/metadata/dsl.rb +0 -111
- data/lib/imw/metadata/field.rb +0 -37
- data/lib/imw/metadata/has_metadata.rb +0 -98
- data/lib/imw/metadata/has_summary.rb +0 -57
- data/lib/imw/metadata/schema.rb +0 -17
- data/lib/imw/parsers.rb +0 -8
- data/lib/imw/parsers/flat.rb +0 -44
- data/lib/imw/parsers/html_parser.rb +0 -387
- data/lib/imw/parsers/html_parser/matchers.rb +0 -289
- data/lib/imw/parsers/line_parser.rb +0 -87
- data/lib/imw/parsers/regexp_parser.rb +0 -72
- data/lib/imw/repository.rb +0 -12
- data/lib/imw/runner.rb +0 -118
- data/lib/imw/schemes.rb +0 -23
- data/lib/imw/schemes/ftp.rb +0 -142
- data/lib/imw/schemes/hdfs.rb +0 -251
- data/lib/imw/schemes/http.rb +0 -165
- data/lib/imw/schemes/local.rb +0 -409
- data/lib/imw/schemes/remote.rb +0 -119
- data/lib/imw/schemes/s3.rb +0 -143
- data/lib/imw/schemes/sql.rb +0 -129
- data/lib/imw/tools.rb +0 -12
- data/lib/imw/tools/aggregator.rb +0 -148
- data/lib/imw/tools/archiver.rb +0 -220
- data/lib/imw/tools/downloader.rb +0 -63
- data/lib/imw/tools/extension_analyzer.rb +0 -114
- data/lib/imw/tools/summarizer.rb +0 -83
- data/lib/imw/tools/transferer.rb +0 -167
- data/lib/imw/utils.rb +0 -74
- data/lib/imw/utils/dynamically_extendable.rb +0 -137
- data/lib/imw/utils/error.rb +0 -59
- data/lib/imw/utils/extensions/hpricot.rb +0 -34
- data/lib/imw/utils/has_uri.rb +0 -131
- data/lib/imw/utils/log.rb +0 -92
- data/lib/imw/utils/misc.rb +0 -57
- data/lib/imw/utils/paths.rb +0 -146
- data/lib/imw/utils/uri.rb +0 -59
- data/lib/imw/utils/uuid.rb +0 -33
- data/lib/imw/utils/validate.rb +0 -38
- data/lib/imw/utils/version.rb +0 -11
- data/spec/data/formats/delimited/sample.csv +0 -131
- data/spec/data/formats/delimited/sample.tsv +0 -131
- data/spec/data/formats/delimited/with_schema/ace-hardware-locations.tsv +0 -11
- data/spec/data/formats/delimited/with_schema/all-countries-ip-address-to-geolocation-data.tsv +0 -16
- data/spec/data/formats/delimited/with_schema/complete-list-of-starbucks-locations.tsv +0 -11
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-cumulative-word-count-from-from-dec.tsv +0 -22
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-myspace-application-adds-by-zip-cod.tsv +0 -22
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-myspace-application-counts.tsv +0 -12
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-user-count-by-latlong.tsv +0 -13
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-user-count-by-zip-code.tsv +0 -22
- data/spec/data/formats/delimited/with_schema/myspace-user-activity-stream-word-count-by-day-from-december-200.tsv +0 -22
- data/spec/data/formats/delimited/without_schema/ace-hardware-locations.tsv +0 -10
- data/spec/data/formats/delimited/without_schema/all-countries-ip-address-to-geolocation-data.tsv +0 -15
- data/spec/data/formats/delimited/without_schema/complete-list-of-starbucks-locations.tsv +0 -10
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-cumulative-word-count-from-from-dec.tsv +0 -21
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-myspace-application-adds-by-zip-cod.tsv +0 -21
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-myspace-application-counts.tsv +0 -11
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-user-count-by-latlong.tsv +0 -12
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-user-count-by-zip-code.tsv +0 -21
- data/spec/data/formats/delimited/without_schema/myspace-user-activity-stream-word-count-by-day-from-december-200.tsv +0 -21
- data/spec/data/formats/excel/sample.xls +0 -0
- data/spec/data/formats/json/sample.json +0 -1
- data/spec/data/formats/none/sample +0 -650
- data/spec/data/formats/sgml/sample.xml +0 -617
- data/spec/data/formats/text/sample.txt +0 -650
- data/spec/data/formats/yaml/sample.yaml +0 -410
- data/spec/data/schema-tabular.yaml +0 -11
- data/spec/imw/archives/rar_spec.rb +0 -16
- data/spec/imw/archives/tar_spec.rb +0 -16
- data/spec/imw/archives/tarbz2_spec.rb +0 -24
- data/spec/imw/archives/targz_spec.rb +0 -21
- data/spec/imw/archives/zip_spec.rb +0 -16
- data/spec/imw/archives_spec.rb +0 -77
- data/spec/imw/compressed_files/bz2_spec.rb +0 -15
- data/spec/imw/compressed_files/compressible_spec.rb +0 -36
- data/spec/imw/compressed_files/gz_spec.rb +0 -15
- data/spec/imw/compressed_files_spec.rb +0 -47
- data/spec/imw/dataset/paths_spec.rb +0 -32
- data/spec/imw/dataset/workflow_spec.rb +0 -41
- data/spec/imw/formats/delimited_spec.rb +0 -44
- data/spec/imw/formats/excel_spec.rb +0 -55
- data/spec/imw/formats/json_spec.rb +0 -18
- data/spec/imw/formats/sgml_spec.rb +0 -24
- data/spec/imw/formats/yaml_spec.rb +0 -19
- data/spec/imw/metadata/contains_metadata_spec.rb +0 -56
- data/spec/imw/metadata/field_spec.rb +0 -25
- data/spec/imw/metadata/has_metadata_spec.rb +0 -58
- data/spec/imw/metadata/has_summary_spec.rb +0 -32
- data/spec/imw/metadata/schema_spec.rb +0 -24
- data/spec/imw/metadata_spec.rb +0 -86
- data/spec/imw/parsers/line_parser_spec.rb +0 -96
- data/spec/imw/parsers/regexp_parser_spec.rb +0 -42
- data/spec/imw/resource_spec.rb +0 -32
- data/spec/imw/schemes/hdfs_spec.rb +0 -67
- data/spec/imw/schemes/http_spec.rb +0 -19
- data/spec/imw/schemes/local_spec.rb +0 -165
- data/spec/imw/schemes/remote_spec.rb +0 -38
- data/spec/imw/schemes/s3_spec.rb +0 -31
- data/spec/imw/schemes/sql_spec.rb +0 -3
- data/spec/imw/tools/aggregator_spec.rb +0 -71
- data/spec/imw/tools/archiver_spec.rb +0 -120
- data/spec/imw/tools/extension_analyzer_spec.rb +0 -153
- data/spec/imw/tools/summarizer_spec.rb +0 -8
- data/spec/imw/tools/transferer_spec.rb +0 -195
- data/spec/imw/utils/dynamically_extendable_spec.rb +0 -69
- data/spec/imw/utils/has_uri_spec.rb +0 -61
- data/spec/imw/utils/paths_spec.rb +0 -10
- data/spec/imw/utils/shared_paths_spec.rb +0 -29
- data/spec/imw_spec.rb +0 -14
- data/spec/rcov.opts +0 -1
- data/spec/spec_helper.rb +0 -31
- data/spec/support/custom_matchers.rb +0 -28
- data/spec/support/file_contents_matcher.rb +0 -30
- data/spec/support/paths_matcher.rb +0 -66
- data/spec/support/random.rb +0 -213
- data/spec/support/without_regard_to_order_matcher.rb +0 -41
data/lib/imw/tools/transferer.rb
DELETED
@@ -1,167 +0,0 @@
|
|
1
|
-
module IMW
|
2
|
-
module Tools
|
3
|
-
|
4
|
-
# A class to encapsulate transferring a resource from one URI to
|
5
|
-
# another.
|
6
|
-
class Transferer
|
7
|
-
|
8
|
-
# The action this Transferer is to take.
|
9
|
-
#
|
10
|
-
# @return [:cp, :mv]
|
11
|
-
attr_reader :action
|
12
|
-
|
13
|
-
# Set the action of this Transferer.
|
14
|
-
#
|
15
|
-
# Will raise an error unless +the_action+ is <tt>:cp</tt> or
|
16
|
-
# <tt>:mv</tt>.
|
17
|
-
#
|
18
|
-
# @param [:cp, :mv] the_action
|
19
|
-
def action= the_action
|
20
|
-
@action = case the_action.to_sym
|
21
|
-
when :cp, :copy then :cp
|
22
|
-
when :mv, :move, :mv! then :mv
|
23
|
-
else raise IMW::ArgumentError.new("action (#{the_action}) must be one of `cp' (or `copy') or `mv' (or `move' or `mv!'")
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# The source resource.
|
28
|
-
#
|
29
|
-
# @return [IMW::Resource]
|
30
|
-
attr_reader :source
|
31
|
-
|
32
|
-
# Set the source for this transferer.
|
33
|
-
#
|
34
|
-
# If +the_source+ is local, will check that it exists and raise
|
35
|
-
# an error if not.
|
36
|
-
#
|
37
|
-
# @param [String, IMW::Resource] the_source
|
38
|
-
def source= the_source
|
39
|
-
s = IMW.open(the_source)
|
40
|
-
s.should_exist!("Cannot #{action_verb}") if s.is_local?
|
41
|
-
@source = s
|
42
|
-
end
|
43
|
-
|
44
|
-
# The destination resource.
|
45
|
-
#
|
46
|
-
# @return [IMW::Resource]
|
47
|
-
attr_reader :destination
|
48
|
-
|
49
|
-
# Set the destination for this transferer.
|
50
|
-
#
|
51
|
-
# If +the_destination+ is local, will check that its parent
|
52
|
-
# directory exists and raise an error if not.
|
53
|
-
def destination= the_destination
|
54
|
-
d = IMW.open(the_destination)
|
55
|
-
d.dir.should_exist!("Cannot #{action_verb}") if d.is_local?
|
56
|
-
@destination = d
|
57
|
-
end
|
58
|
-
|
59
|
-
# Instantiate a new transferer to take the given +action+ on
|
60
|
-
# +source+ and +destination+.
|
61
|
-
#
|
62
|
-
# @param [:cp, :mv] action the action to take
|
63
|
-
# @param [String, IMW::Resource] source
|
64
|
-
# @param [String, IMW::Resource] destination
|
65
|
-
def initialize action, source, destination
|
66
|
-
self.action = action
|
67
|
-
self.source = source
|
68
|
-
self.destination = destination
|
69
|
-
raise IMW::PathError.new("Source and destination have the same URI: #{source}") if self.source.uri.to_s == self.destination.uri.to_s
|
70
|
-
end
|
71
|
-
|
72
|
-
# Transfer source to destination.
|
73
|
-
#
|
74
|
-
# For local transfer, will raise errors unless the necessary
|
75
|
-
# paths exist.
|
76
|
-
def transfer!
|
77
|
-
IMW.announce_if_verbose("#{action_gerund.capitalize} #{source} to #{destination}")
|
78
|
-
send(transfer_method)
|
79
|
-
destination.reopen
|
80
|
-
end
|
81
|
-
|
82
|
-
protected
|
83
|
-
|
84
|
-
# Return the name of the method that should be used to transfer
|
85
|
-
# +source+ to +destination+.
|
86
|
-
#
|
87
|
-
# @return [String]
|
88
|
-
def transfer_method
|
89
|
-
source_scheme = source.is_local? ? 'file' : source.scheme
|
90
|
-
destination_scheme = destination.is_local? ? 'file' : destination.scheme
|
91
|
-
method = "#{source_scheme}_to_#{destination_scheme}"
|
92
|
-
raise IMW::NoMethodError.new("Do not know how to #{action_verb} #{source} to #{destination}") unless respond_to?(method)
|
93
|
-
method
|
94
|
-
end
|
95
|
-
|
96
|
-
def action_verb # :nodoc
|
97
|
-
action == :cp ? "copy" : "move"
|
98
|
-
end
|
99
|
-
|
100
|
-
def action_gerund # :nodoc
|
101
|
-
action == :cp ? "copying" : "moving"
|
102
|
-
end
|
103
|
-
|
104
|
-
#
|
105
|
-
# Purely local file
|
106
|
-
#
|
107
|
-
|
108
|
-
def file_to_file
|
109
|
-
fu_action = (action == :cp && source.is_directory?) ? :cp_r : action
|
110
|
-
FileUtils.send(fu_action, source.path, destination.path)
|
111
|
-
end
|
112
|
-
|
113
|
-
#
|
114
|
-
# HTTP
|
115
|
-
#
|
116
|
-
|
117
|
-
def http_to_file
|
118
|
-
File.open(destination.path, 'w') { |f| f.write(source.read) }
|
119
|
-
end
|
120
|
-
|
121
|
-
#
|
122
|
-
# S3
|
123
|
-
#
|
124
|
-
|
125
|
-
def file_to_s3
|
126
|
-
IMW::Schemes::S3.put(source, destination)
|
127
|
-
end
|
128
|
-
|
129
|
-
def http_to_s3
|
130
|
-
IMW::Schemes::S3.put(source, destination)
|
131
|
-
end
|
132
|
-
|
133
|
-
def s3_to_file
|
134
|
-
IMW::Schemes::S3.get(source, destination)
|
135
|
-
end
|
136
|
-
|
137
|
-
def s3_to_s3
|
138
|
-
IMW::Schemes::S3.copy(source, destination)
|
139
|
-
end
|
140
|
-
|
141
|
-
#
|
142
|
-
# HDFS
|
143
|
-
#
|
144
|
-
|
145
|
-
def hdfs_to_hdfs
|
146
|
-
IMW::Schemes::HDFS.fs(action, source.path, destination.path)
|
147
|
-
end
|
148
|
-
|
149
|
-
def file_to_hdfs
|
150
|
-
IMW::Schemes::HDFS.fs(:put, source.path, destination.path)
|
151
|
-
end
|
152
|
-
|
153
|
-
def hdfs_to_file
|
154
|
-
IMW::Schemes::HDFS.fs(:get, source.path, destination.path)
|
155
|
-
end
|
156
|
-
|
157
|
-
def s3_to_hdfs
|
158
|
-
IMW::Schemes::HDFS.fs(action, source.s3n_url, destination.path)
|
159
|
-
end
|
160
|
-
|
161
|
-
def hdfs_to_s3
|
162
|
-
IMW::Schemes::HDFS.fs(action, source.path, destination.s3n_url)
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
data/lib/imw/utils.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'fileutils'
|
3
|
-
require 'active_support/core_ext/object/blank'
|
4
|
-
require 'active_support/core_ext/hash/reverse_merge'
|
5
|
-
require 'active_support/core_ext/string/starts_ends_with'
|
6
|
-
require 'imw/utils/error'
|
7
|
-
require 'imw/utils/log'
|
8
|
-
require 'imw/utils/paths'
|
9
|
-
require 'imw/utils/misc'
|
10
|
-
|
11
|
-
module IMW
|
12
|
-
|
13
|
-
# Utility modules.
|
14
|
-
module Utils
|
15
|
-
autoload :DynamicallyExtendable, 'imw/utils/dynamically_extendable'
|
16
|
-
autoload :HasURI, 'imw/utils/has_uri'
|
17
|
-
end
|
18
|
-
|
19
|
-
# A replacement for the standard system call which raises an
|
20
|
-
# IMW::SystemCallError if the command fails which prints better
|
21
|
-
# debugging info.
|
22
|
-
#
|
23
|
-
# This function relies upon Kernel.system and obeys the same rules:
|
24
|
-
#
|
25
|
-
# - if +commands+ has only only a single element then no shell
|
26
|
-
# characters or spaces are escaped -- you have to do it yourself
|
27
|
-
# or you get to use shell characters, depending on your
|
28
|
-
# perspective.
|
29
|
-
#
|
30
|
-
# - if +commands+ is a list of elements then the second and further
|
31
|
-
# elements in the list have their shell characters and spaces
|
32
|
-
# escaped
|
33
|
-
#
|
34
|
-
# But it also has its own rules:
|
35
|
-
#
|
36
|
-
# - When one of the +commands+ is an empty or blank string,
|
37
|
-
# Kernel.system honors it and escapes it properly and sends it
|
38
|
-
# along for evaluation. This can be a problem for some programs
|
39
|
-
# and so IMW.system excludes blank (as in <tt>blank?</tt>)
|
40
|
-
# elements of +commands+.
|
41
|
-
#
|
42
|
-
# - +commands+ will be flattened (see the gotcha below)
|
43
|
-
#
|
44
|
-
# Calling out to the shell like this is often brittle. Imagine
|
45
|
-
# defining
|
46
|
-
#
|
47
|
-
# prog = 'some_prog'
|
48
|
-
# flags = '-v -f'
|
49
|
-
# args = 'file.txt'
|
50
|
-
#
|
51
|
-
# and later calling
|
52
|
-
#
|
53
|
-
# IMW.system prog, flags, args
|
54
|
-
#
|
55
|
-
# The space in the second argument ('-v -f') will be escaped and
|
56
|
-
# will therefore not be properly parsed by +some_prog+. Instead try
|
57
|
-
#
|
58
|
-
# prog = 'some_prog'
|
59
|
-
# flags = ['-v', '-f']
|
60
|
-
# args = ['file.txt']
|
61
|
-
#
|
62
|
-
# IMW.system prog, flags, *args
|
63
|
-
#
|
64
|
-
# which will work fine since +flags+ will automatically be flattend.
|
65
|
-
def self.system *commands
|
66
|
-
stripped_commands = commands.flatten.map { |command| command.to_s unless command.blank? }.compact
|
67
|
-
IMW.announce_if_verbose(stripped_commands.join(" "))
|
68
|
-
exit_code = Kernel.system(*stripped_commands)
|
69
|
-
raise IMW::SystemCallError.new($?.dup, commands.join(' ')) unless $?.success?
|
70
|
-
exit_code
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
@@ -1,137 +0,0 @@
|
|
1
|
-
module IMW
|
2
|
-
module Utils
|
3
|
-
|
4
|
-
# Provides an including class with a class-level array of
|
5
|
-
# "handlers" that it can use to dynamically extend its instances
|
6
|
-
# with specific modules only if certain conditions are met.
|
7
|
-
#
|
8
|
-
# This allows different instances of a class to implement very
|
9
|
-
# different behavior at runtime.
|
10
|
-
#
|
11
|
-
# An example use case might be a Database class which dynamically
|
12
|
-
# extends its instances with an adaptor module appropriate to the
|
13
|
-
# particular database the object refers to.
|
14
|
-
module DynamicallyExtendable
|
15
|
-
|
16
|
-
def self.included obj
|
17
|
-
obj.extend(ClassMethods)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Return the modules this object has been extended by.
|
21
|
-
#
|
22
|
-
# @return [Array]
|
23
|
-
def modules
|
24
|
-
@modules ||= []
|
25
|
-
end
|
26
|
-
|
27
|
-
# Works just like Object#extend except it keeps track of the
|
28
|
-
# modules it has extended.
|
29
|
-
#
|
30
|
-
# @see IMW::Utils::DynamicallyExtendable#modules
|
31
|
-
def extend mod
|
32
|
-
modules << mod
|
33
|
-
super mod
|
34
|
-
end
|
35
|
-
|
36
|
-
# Iterate through this object's class's handlers and extend this
|
37
|
-
# object with the module referred to by any matching handlers.
|
38
|
-
def extend_appropriately! options={}
|
39
|
-
self.class.extend_instance! self, options
|
40
|
-
end
|
41
|
-
|
42
|
-
# A collection of methods which provide a class including
|
43
|
-
# IMW::Utils::DynamicallyExtendable with a class-level Array of
|
44
|
-
# handlers that can be applied to instances by calling the
|
45
|
-
# instance's +extend_appropriately!+ method.
|
46
|
-
module ClassMethods
|
47
|
-
|
48
|
-
# The handlers an including class has defined.
|
49
|
-
#
|
50
|
-
# @return [Array<Array>]
|
51
|
-
def handlers
|
52
|
-
@handlers ||= []
|
53
|
-
end
|
54
|
-
|
55
|
-
# Register a new handler for an including class.
|
56
|
-
#
|
57
|
-
# +handler+ must be one of
|
58
|
-
#
|
59
|
-
# 1. Regexp
|
60
|
-
# 2. Proc
|
61
|
-
# 3. +true+
|
62
|
-
#
|
63
|
-
# In case (1), if the regular expression matches the
|
64
|
-
# instance's +to_s+ method then the module (+mod+) will be
|
65
|
-
# used..
|
66
|
-
#
|
67
|
-
# In case (2), if the Proc returns a value other than +false+
|
68
|
-
# or +nil+ after being passed an instance then the module will
|
69
|
-
# be used.
|
70
|
-
#
|
71
|
-
# In case (3), the module will be used.
|
72
|
-
#
|
73
|
-
# @param [String, Module] mod
|
74
|
-
# @param [Regexp, Proc, true] handler
|
75
|
-
def register_handler mod, handler
|
76
|
-
raise IMW::ArgumentError.new("Module must be either a Module or String") unless mod.is_a?(Module) || mod.is_a?(String)
|
77
|
-
raise IMW::ArgumentError.new("Handler must be either a Regexp, Proc, or true") unless handler.is_a?(Regexp) || handler.is_a?(Proc) || handler == true
|
78
|
-
handlers << [mod, handler]
|
79
|
-
end
|
80
|
-
|
81
|
-
# Register a collection of handlers.
|
82
|
-
#
|
83
|
-
# @see IMW::Utils::DynamicallyExtendable::ClassMethods#register_handler
|
84
|
-
def register_handlers *pairs
|
85
|
-
pairs.each { |pair| register_handler *pair }
|
86
|
-
end
|
87
|
-
|
88
|
-
# Iterate through this class's handlers and extend the given
|
89
|
-
# object with modules whose handler conditions match the
|
90
|
-
# instance.
|
91
|
-
#
|
92
|
-
# Passing in <tt>:use_modules</tt> or <tt>:skip_modules</tt>
|
93
|
-
# allows overriding the default behavior of handlers.
|
94
|
-
#
|
95
|
-
# @param [Object] instance
|
96
|
-
# @param [Hash] options
|
97
|
-
# @option options [Array<String,Module>] use_modules a list of modules used regardless of handlers
|
98
|
-
# @option options [Array<String,Module>] skip_modules a list of modules not to be used regardless of handlers
|
99
|
-
# @return [Object] the newly extended object
|
100
|
-
def extend_instance! instance, options={}
|
101
|
-
return if options[:no_modules]
|
102
|
-
options.reverse_merge!(:use_modules => [], :skip_modules => [])
|
103
|
-
handlers.each do |mod_name, handler|
|
104
|
-
case handler
|
105
|
-
when Regexp then extend_instance_with_mod_or_string!(instance, mod_name, options[:skip_modules]) if handler =~ instance.to_s
|
106
|
-
when Proc then extend_instance_with_mod_or_string!(instance, mod_name, options[:skip_modules]) if handler.call(instance)
|
107
|
-
when TrueClass then extend_instance_with_mod_or_string!(instance, mod_name, options[:skip_modules])
|
108
|
-
else raise IMW::TypeError("A handler must be Regexp, Proc, or true")
|
109
|
-
end
|
110
|
-
end
|
111
|
-
options[:use_modules].each { |mod_name| extend_instance_with_mod_or_string!(instance, mod_name, options[:skip_modules]) }
|
112
|
-
instance
|
113
|
-
end
|
114
|
-
|
115
|
-
# Extend +instance+ with +mod_or_string+. Will work hard to
|
116
|
-
# try and interpret +mod_or_string+ as a module if it's a
|
117
|
-
# string.
|
118
|
-
#
|
119
|
-
# @param [Object] instance
|
120
|
-
#
|
121
|
-
# @param [Module, String] mod_or_string the module or string
|
122
|
-
# representing a module to extend the instance with
|
123
|
-
#
|
124
|
-
# @param [Array<Module,String>] skip_modules modules to exclude
|
125
|
-
def extend_instance_with_mod_or_string! instance, mod_or_string, skip_modules
|
126
|
-
return if skip_modules.include?(mod_or_string)
|
127
|
-
if mod_or_string.is_a?(Module)
|
128
|
-
instance.extend(mod_or_string)
|
129
|
-
else
|
130
|
-
m = IMW.class_eval(mod_or_string)
|
131
|
-
instance.extend(m) unless skip_modules.include?(m)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
data/lib/imw/utils/error.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
module IMW
|
2
|
-
|
3
|
-
# Base error class which all IMW errors subclass.
|
4
|
-
Error = Class.new(StandardError)
|
5
|
-
|
6
|
-
# Method undefined.
|
7
|
-
NoMethodError = Class.new(Error)
|
8
|
-
|
9
|
-
# Type error.
|
10
|
-
TypeError = Class.new(Error)
|
11
|
-
|
12
|
-
# Not implemented (typically because user needs to define a method
|
13
|
-
# when subclassing a base class).
|
14
|
-
NotImplementedError = Class.new(Error)
|
15
|
-
|
16
|
-
# Error during parsing.
|
17
|
-
ParseError = Class.new(Error)
|
18
|
-
|
19
|
-
# Error with a non-existing, invalid, or inaccessible path.
|
20
|
-
PathError = Class.new(Error)
|
21
|
-
|
22
|
-
# Error communicating with a remote entity.
|
23
|
-
NetworkError = Class.new(Error)
|
24
|
-
|
25
|
-
# Raised when a resource is of the wrong scheme.
|
26
|
-
SchemeError = Class.new(Error)
|
27
|
-
|
28
|
-
# Raised when a resource is of the wrong (or malformed) format.
|
29
|
-
FormatError = Class.new(Error)
|
30
|
-
|
31
|
-
# Bad argument.
|
32
|
-
ArgumentError = Class.new(Error)
|
33
|
-
|
34
|
-
# Error in defining or matching a schema.
|
35
|
-
SchemaError = Class.new(Error)
|
36
|
-
|
37
|
-
# An error meant to be used when a system call goes awry. It will
|
38
|
-
# report exit status and the process id of the offending call.
|
39
|
-
class SystemCallError < IMW::Error
|
40
|
-
|
41
|
-
attr_reader :status, :message
|
42
|
-
|
43
|
-
def initialize(status, message)
|
44
|
-
@status = status
|
45
|
-
@message = message
|
46
|
-
end
|
47
|
-
|
48
|
-
def display
|
49
|
-
"(error code: #{status.exitstatus}, pid: #{status.pid}) #{message}"
|
50
|
-
end
|
51
|
-
|
52
|
-
def to_s
|
53
|
-
"(error code: #{status.exitstatus}, pid: #{status.pid}) #{message}"
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'hpricot'
|
2
|
-
|
3
|
-
module Hpricot::IMWExtensions
|
4
|
-
|
5
|
-
# Return the contents of the first element to match +path+.
|
6
|
-
def contents_of path
|
7
|
-
cnts = self.at path
|
8
|
-
cnts.inner_html if cnts
|
9
|
-
end
|
10
|
-
|
11
|
-
# Return the value of +attr+ for the first element to match +path+.
|
12
|
-
def path_attr path, attr
|
13
|
-
cnts = self.at path
|
14
|
-
cnts.attributes[attr] if cnts
|
15
|
-
end
|
16
|
-
|
17
|
-
# Return the value of the +class+ attribute of the first element to
|
18
|
-
# match +path+.
|
19
|
-
def class_of path
|
20
|
-
self.path_attr(path, 'class')
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Hpricot::Elem
|
25
|
-
include Hpricot::IMWExtensions
|
26
|
-
end
|
27
|
-
|
28
|
-
class Hpricot::Elements
|
29
|
-
include Hpricot::IMWExtensions
|
30
|
-
end
|
31
|
-
|
32
|
-
class Hpricot::Doc
|
33
|
-
include Hpricot::IMWExtensions
|
34
|
-
end
|