right_aws 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +47 -0
- data/Manifest.txt +1 -0
- data/README.txt +65 -48
- data/Rakefile +11 -6
- data/lib/awsbase/right_awsbase.rb +94 -25
- data/lib/awsbase/support.rb +106 -0
- data/lib/ec2/right_ec2.rb +257 -140
- data/lib/right_aws.rb +4 -2
- data/lib/s3/right_s3.rb +39 -24
- data/lib/s3/right_s3_interface.rb +13 -16
- data/lib/sqs/right_sqs.rb +30 -19
- data/lib/sqs/right_sqs_interface.rb +49 -43
- data/test/ec2/test_right_ec2.rb +3 -2
- data/test/s3/test_right_s3.rb +1 -1
- data/test/sqs/test_right_sqs.rb +19 -24
- data/test/ts_right_aws.rb +1 -0
- metadata +6 -22
- data/test/awsbase/test_helper.rb +0 -2
- data/test/awsbase/test_right_awsbase.rb +0 -12
data/History.txt
CHANGED
@@ -1,3 +1,50 @@
|
|
1
1
|
== 1.1.0 / 2007-08-10
|
2
2
|
Initial release.
|
3
3
|
|
4
|
+
== 1.2.0 / 2007-09-12
|
5
|
+
------------------------------------------------------------------------
|
6
|
+
r1718 | todd | 2007-09-12 15:34:37 -0700 (Wed, 12 Sep 2007) | 3 lines
|
7
|
+
|
8
|
+
(#458) Extensive documentation review, rework, and expansion. Also added
|
9
|
+
coverage analysis to the test suite using RCov.
|
10
|
+
|
11
|
+
------------------------------------------------------------------------
|
12
|
+
r1690 | todd | 2007-09-07 15:23:11 -0700 (Fri, 07 Sep 2007) | 2 lines
|
13
|
+
|
14
|
+
(#447) Add support.rb to manifest
|
15
|
+
|
16
|
+
------------------------------------------------------------------------
|
17
|
+
r1688 | todd | 2007-09-07 13:57:39 -0700 (Fri, 07 Sep 2007) | 4 lines
|
18
|
+
|
19
|
+
#(447) Use Active Support if available, but don't require it. Load our own
|
20
|
+
extensions if it's not present. This keeps us from overloading ActiveSupport if
|
21
|
+
a user's already using it.
|
22
|
+
|
23
|
+
------------------------------------------------------------------------
|
24
|
+
r1687 | todd | 2007-09-07 11:36:43 -0700 (Fri, 07 Sep 2007) | 2 lines
|
25
|
+
|
26
|
+
(#447) Removed dependency on activesupport
|
27
|
+
|
28
|
+
------------------------------------------------------------------------
|
29
|
+
r1676 | konstantin | 2007-09-06 01:27:09 -0700 (Thu, 06 Sep 2007) | 1 line
|
30
|
+
|
31
|
+
paid AMIs, small fix
|
32
|
+
------------------------------------------------------------------------
|
33
|
+
r1667 | konstantin | 2007-09-05 12:58:10 -0700 (Wed, 05 Sep 2007) | 1 line
|
34
|
+
|
35
|
+
#427, Gems, paid AMI support for ec2_instances
|
36
|
+
------------------------------------------------------------------------
|
37
|
+
r1658 | konstantin | 2007-09-05 01:02:25 -0700 (Wed, 05 Sep 2007) | 1 line
|
38
|
+
|
39
|
+
RightGems: a couple of params improvements, now ones can use a String as well as an Array.
|
40
|
+
------------------------------------------------------------------------
|
41
|
+
r1653 | konstantin | 2007-09-04 12:31:19 -0700 (Tue, 04 Sep 2007) | 1 line
|
42
|
+
|
43
|
+
Right gems: libxml and paid AMI support added
|
44
|
+
------------------------------------------------------------------------
|
45
|
+
r1581 | tve | 2007-08-24 16:21:45 -0700 (Fri, 24 Aug 2007) | 1 line
|
46
|
+
|
47
|
+
Improved RightAws documentation
|
48
|
+
------------------------------------------------------------------------
|
49
|
+
|
50
|
+
|
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1,56 +1,74 @@
|
|
1
|
-
|
2
|
-
by RightScale, Inc.
|
3
|
-
www.RightScale.com
|
1
|
+
= RightScale Amazon Web Services Ruby Gems
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
RightAws::Ec2 is a Ruby library for the Amazon EC2 (Elastic Compute Cloud)
|
8
|
-
service.
|
3
|
+
Published by RightScale, Inc. under the MIT License.
|
4
|
+
For information about RightScale, see http://www.rightscale.com
|
9
5
|
|
10
|
-
|
11
|
-
(Simple Storage Service) service.
|
12
|
-
|
13
|
-
RightAws::Sqs and RightAws::SqsInterface is a Ruby library for the Amazon SQS (Simple Queue Service)
|
14
|
-
service.
|
6
|
+
== DESCRIPTION:
|
15
7
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
8
|
+
The RightScale AWS gems have been designed to provide a robust, fast, and secure interface to Amazon EC2, Amazon, S3, and Amazon SQS. These gems have been used in production by RightScale since late 2006 and are being maintained to track enhancements made by Amazon. The RightScale AWS gems comprise:
|
9
|
+
|
10
|
+
- RightAws::Ec2 -- interface to Amazon EC2 (Elastic Compute Cloud)
|
11
|
+
- RightAws::S3 and RightAws::S3Interface -- interface to Amazon S3 (Simple Storage Service)
|
12
|
+
- RightAws::Sqs and RightAws::SqsInterface -- interface to Amazon SQS (Simple Queue Service)
|
13
|
+
|
14
|
+
== FEATURES:
|
15
|
+
|
16
|
+
- Full programmmatic access to EC2, S3, and SQS.
|
17
|
+
- Complete error handling: all operations check for errors and report complete
|
18
|
+
error information by raising an AwsError.
|
19
|
+
- Persistent HTTP connections with robust network-level retry layer using
|
20
|
+
RightHttpConnection). This includes socket timeouts and retries.
|
21
|
+
- Robust HTTP-level retry layer. Certain (user-adjustable) HTTP errors returned
|
22
|
+
by Amazon's services are classified as temporary errors.
|
23
|
+
These errors are automaticallly retried using exponentially increasing intervals.
|
24
|
+
The number of retries is user-configurable.
|
25
|
+
- Fast REXML-based parsing of responses (as fast as a pure Ruby solution allows).
|
26
|
+
- Uses libxml (if available) for faster response parsing.
|
27
|
+
- Support for large S3 list operations. Buckets and key subfolders containing
|
28
|
+
many (> 1000) keys are listed in entirety. Operations based on list (like
|
29
|
+
bucket clear) work on arbitrary numbers of keys.
|
30
|
+
- Support for streaming GETs from S3, and streaming PUTs to S3 if the data source is a file.
|
31
|
+
- Support for single-threaded usage, multithreaded usage, as well as usage with multiple
|
32
|
+
AWS accounts.
|
33
|
+
- Test suite (requires AWS account to do "live" testing).
|
34
|
+
|
35
|
+
== THREADING:
|
36
|
+
|
37
|
+
All RightScale AWS interfaces offer two threading options:
|
38
|
+
1. Use a single persistent HTTP connection per process.
|
39
|
+
2. Use a persistent HTTP connection per Ruby thread.
|
40
|
+
|
41
|
+
Either way, it doesn't matter how many (for example) RightAws::S3 objects you create,
|
42
|
+
they all use the same per-program or per-thread
|
21
43
|
connection. The purpose of sharing the connection is to keep a single
|
22
44
|
persistent HTTP connection open to avoid paying connection
|
23
45
|
overhead on every request. However, if you have multiple concurrent
|
24
46
|
threads, you may want or need an HTTP connection per thread to enable
|
25
|
-
concurrent requests to
|
26
|
-
1
|
27
|
-
2
|
28
|
-
concurrent requests to S3 (SQS, EC2).
|
29
|
-
3
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
47
|
+
concurrent requests to AWS. The way this plays out in practice is:
|
48
|
+
1. If you have a non-multithreaded Ruby program, use the non-multithreaded setting.
|
49
|
+
2. If you have a multi-threaded Ruby program, use the multithreaded setting to enable
|
50
|
+
concurrent requests to S3 (or SQS, or EC2).
|
51
|
+
3. For running under Mongrel/Rails, use the non-multithreaded setting even though
|
52
|
+
mongrel is multithreaded. This is because only one Rails handler is invoked at
|
53
|
+
time (i.e. it acts like a single-threaded program)
|
54
|
+
|
55
|
+
Note that due to limitations in the I/O of the Ruby interpreter you
|
56
|
+
may not get the degree of parallelism you may expect with the multi-threaded setting.
|
57
|
+
|
58
|
+
By default, EC2/S3/SQS interface instances are created in single-threaded mode. Set
|
34
59
|
"params[:multi_thread]" to "true" in the initialization arguments to use
|
35
60
|
multithreaded mode.
|
36
61
|
|
37
|
-
==
|
62
|
+
== GETTING STARTED:
|
38
63
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
user-configurable.
|
46
|
-
- Support for large S3 list operations. Buckets and key subfolders containing
|
47
|
-
many (> 1000) keys are listed in entirety. Operations based on list (like
|
48
|
-
bucket clear) work on arbitrary numbers of keys.
|
49
|
-
- Support for streaming PUTs to S3 if the data source is a file.
|
50
|
-
- Support for streaming GETs from S3.
|
51
|
-
- Interfaces for HTML link generation.
|
64
|
+
* For EC2 read RightAws::Ec2 and consult the Amazon EC2 API documentation at
|
65
|
+
http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=87
|
66
|
+
* For S3 read RightAws::S3 and consult the Amazon S3 API documentation at
|
67
|
+
http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=48
|
68
|
+
* For SQS read RightAws::Sqs and consult the Amazon SQS API documentation at
|
69
|
+
http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=31
|
52
70
|
|
53
|
-
|
71
|
+
== KNOWN ISSUES:
|
54
72
|
|
55
73
|
- Amazon recently (8/07) changed the semantics of the SQS service. A
|
56
74
|
new queue may not be created within 60 seconds of the destruction of any
|
@@ -58,15 +76,14 @@ Known Problems:
|
|
58
76
|
RightAws::SqsInterface will fail with the message:
|
59
77
|
"AWS.SimpleQueueService.QueueDeletedRecently: You must wait 60 seconds after deleting a queue before you can create another with the same name."
|
60
78
|
|
61
|
-
== SYNOPSIS:
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
79
|
== REQUIREMENTS:
|
68
80
|
|
69
|
-
RightAws requires
|
81
|
+
RightAws requires REXML and the RightHttpConnection gem.
|
82
|
+
If libxml and its Ruby bindings (distributed in the libxml-ruby gem) are
|
83
|
+
present, RightAws can be configured to use them:
|
84
|
+
RightAws::RightAWSParser.xml_lib = 'libxml'
|
85
|
+
Any error with the libxml installation will result in RightAws failing-safe to
|
86
|
+
REXML parsing.
|
70
87
|
|
71
88
|
== INSTALL:
|
72
89
|
|
data/Rakefile
CHANGED
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'hoe'
|
5
|
-
require
|
5
|
+
require "rake/testtask"
|
6
|
+
require 'rcov/rcovtask'
|
7
|
+
$: << File.dirname(__FILE__)
|
8
|
+
require 'lib/right_aws.rb'
|
6
9
|
|
7
10
|
Hoe.new('right_aws', RightAws::VERSION::STRING) do |p|
|
8
11
|
p.rubyforge_name = 'rightaws'
|
@@ -13,13 +16,15 @@ Hoe.new('right_aws', RightAws::VERSION::STRING) do |p|
|
|
13
16
|
p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
14
17
|
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
15
18
|
p.remote_rdoc_dir = "/right_aws_gem_doc"
|
16
|
-
p.extra_deps = [['
|
17
|
-
|
19
|
+
p.extra_deps = [['right_http_connection','>= 0.1.4']]
|
20
|
+
p.test_globs = ['test/ts_right_aws.rb']
|
18
21
|
end
|
19
22
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
desc "Analyze code coverage of the unit tests."
|
24
|
+
Rcov::RcovTask.new do |t|
|
25
|
+
t.test_files = FileList["test/ts*.rb"]
|
26
|
+
#t.verbose = true # uncomment to see the executed command
|
23
27
|
end
|
24
28
|
|
29
|
+
|
25
30
|
# vim: syntax=Ruby
|
@@ -21,36 +21,48 @@
|
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#
|
23
23
|
|
24
|
-
|
24
|
+
# Test
|
25
25
|
module RightAws
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
27
|
+
# Text, if found in an error message returned by AWS, indicates that this may be a transient
|
28
|
+
# error. Transient errors are automatically retried with exponential back-off.
|
29
|
+
AMAZON_PROBLEMS = [ 'internal service error',
|
30
|
+
'is currently unavailable',
|
31
|
+
'no response from',
|
32
|
+
'Please try again',
|
33
|
+
'InternalError',
|
34
|
+
'ServiceUnavailable', #from SQS docs
|
35
|
+
'Unavailable',
|
36
|
+
'This application is not currently available',
|
37
|
+
'InsufficientInstanceCapacity'
|
38
|
+
]
|
39
|
+
|
40
|
+
# Exception class to signal any Amazon errors. All errors occuring during calls to Amazon's
|
41
|
+
# web services raise this type of error.
|
42
|
+
# Attribute inherited by RuntimeError:
|
43
|
+
# message - the text of the error, generally as returned by AWS in its XML response.
|
44
|
+
|
44
45
|
class AwsError < RuntimeError
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
|
47
|
+
# either an array of errors where each item is itself an array of [code, message]),
|
48
|
+
# or an error string if the error was raised manually, as in <tt>AwsError.new('err_text')</tt>
|
49
|
+
attr_reader :errors
|
50
|
+
|
51
|
+
# Request id (if exists)
|
52
|
+
attr_reader :request_id
|
53
|
+
|
54
|
+
# Response HTTP error code
|
55
|
+
attr_reader :http_code
|
56
|
+
|
48
57
|
def initialize(errors=nil, http_code=nil, request_id=nil)
|
49
58
|
@errors = errors
|
50
59
|
@request_id = request_id
|
51
60
|
@http_code = http_code
|
52
61
|
super(@errors.is_a?(Array) ? @errors.map{|code, msg| "#{code}: #{msg}"}.join("; ") : @errors.to_s)
|
53
62
|
end
|
63
|
+
|
64
|
+
# Does any of the error messages include the regexp +pattern+?
|
65
|
+
# Used to determine whether to retry request.
|
54
66
|
def include?(pattern)
|
55
67
|
if @errors.is_a?(Array)
|
56
68
|
@errors.each{ |code, msg| return true if code =~ pattern }
|
@@ -60,8 +72,14 @@ module RightAws
|
|
60
72
|
false
|
61
73
|
end
|
62
74
|
|
75
|
+
# Generic handler for AwsErrors. +aws+ is the RightAws::S3, RightAws::EC2, or RightAws::SQS
|
76
|
+
# object that caused the exception (it must provide last_request and last_response). Supported
|
77
|
+
# boolean options are:
|
78
|
+
# * <tt>:log</tt> print a message into the log using aws.logger to access the Logger
|
79
|
+
# * <tt>:puts</tt> do a "puts" of the error
|
80
|
+
# * <tt>:raise</tt> re-raise the error after logging
|
63
81
|
def self.on_aws_exception(aws, options={:raise=>true, :log=>true})
|
64
|
-
|
82
|
+
# Only log & notify if not user error
|
65
83
|
if !options[:raise] || system_error?($!)
|
66
84
|
error_text = "#{$!.inspect}\n#{$@}.join('\n')}"
|
67
85
|
puts error_text if options[:puts]
|
@@ -78,6 +96,8 @@ module RightAws
|
|
78
96
|
return nil
|
79
97
|
end
|
80
98
|
|
99
|
+
# True if e is an AWS system error, i.e. something that is for sure not the caller's fault.
|
100
|
+
# Used to force logging.
|
81
101
|
def self.system_error?(e)
|
82
102
|
!e.is_a?(self) || e.message =~ /InternalError|InsufficientInstanceCapacity|Unavailable/
|
83
103
|
end
|
@@ -127,7 +147,7 @@ module RightAws
|
|
127
147
|
if response.body && response.body[/<\?xml/] # ... it is a xml document
|
128
148
|
@aws.class.bench_xml.add! do
|
129
149
|
error_parser = RightErrorResponseParser.new
|
130
|
-
|
150
|
+
error_parser.parse(response)
|
131
151
|
@aws.last_errors = error_parser.errors
|
132
152
|
@aws.last_request_id = error_parser.requestID
|
133
153
|
last_errors_text = @aws.last_errors.flatten.join("\n")
|
@@ -166,13 +186,25 @@ module RightAws
|
|
166
186
|
|
167
187
|
#-----------------------------------------------------------------
|
168
188
|
|
169
|
-
class RightAWSParser
|
189
|
+
class RightAWSParser #:nodoc:
|
190
|
+
@@xml_lib = 'rexml' # xml library name: 'rexml' | 'libxml'
|
191
|
+
def self.xml_lib
|
192
|
+
@@xml_lib
|
193
|
+
end
|
194
|
+
def self.xml_lib=(new_lib_name)
|
195
|
+
@@xml_lib = new_lib_name
|
196
|
+
end
|
197
|
+
|
170
198
|
attr_accessor :result
|
171
199
|
attr_reader :xmlpath
|
172
|
-
|
200
|
+
attr_accessor :xml_lib
|
201
|
+
|
202
|
+
def initialize(params={})
|
173
203
|
@xmlpath = ''
|
174
204
|
@result = false
|
175
205
|
@text = ''
|
206
|
+
@xml_lib = params[:xml_lib] || @@xml_lib
|
207
|
+
@logger = params[:logger]
|
176
208
|
reset
|
177
209
|
end
|
178
210
|
def tag_start(name, attributes)
|
@@ -188,6 +220,43 @@ module RightAws
|
|
188
220
|
def text(text)
|
189
221
|
@text = text
|
190
222
|
tagtext(text)
|
223
|
+
end
|
224
|
+
# Parser method.
|
225
|
+
# Params:
|
226
|
+
# xml_text - xml message text(String) or Net:HTTPxxx instance (response)
|
227
|
+
# params[:xml_lib] - library name: 'rexml' | 'libxml'
|
228
|
+
def parse(xml_text, params={})
|
229
|
+
# Get response body
|
230
|
+
xml_text = xml_text.body unless xml_text.is_a?(String)
|
231
|
+
@xml_lib = params[:xml_lib] || @xml_lib
|
232
|
+
# load xml library
|
233
|
+
if @xml_lib=='libxml' && !defined?(XML::SaxParser)
|
234
|
+
begin
|
235
|
+
require 'xml/libxml'
|
236
|
+
rescue LoadError => e
|
237
|
+
@xml_lib = 'rexml'
|
238
|
+
if @logger
|
239
|
+
@logger.error e.inspect
|
240
|
+
@logger.error e.backtrace
|
241
|
+
@logger.info "Can not load 'libxml' library. 'Rexml' is used for parsing."
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
# Parse the xml text
|
246
|
+
case @xml_lib
|
247
|
+
when 'libxml'
|
248
|
+
xml = XML::SaxParser.new
|
249
|
+
xml.string = xml_text
|
250
|
+
xml.on_start_element{|name, attr_hash| self.tag_start(name, attr_hash)}
|
251
|
+
xml.on_characters{ |text| self.text(text)}
|
252
|
+
xml.on_end_element{ |name| self.tag_end(name)}
|
253
|
+
xml.parse
|
254
|
+
else
|
255
|
+
if @logger && @xml_lib!='rexml'
|
256
|
+
@logger.info "RightAWSParser#parse: Unknown xml library ('#{@xml_lib}') is selected. The default 'rexml' is used."
|
257
|
+
end
|
258
|
+
REXML::Document.parse_stream(xml_text, self)
|
259
|
+
end
|
191
260
|
end
|
192
261
|
# Parser must have a lots of methods
|
193
262
|
# (see /usr/lib/ruby/1.8/rexml/parsers/streamparser.rb)
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# If ActiveSupport is present, then great - require it and use it. But we
|
2
|
+
# don't want a dependency on it, so if it's not present, define the few
|
3
|
+
# extensions that we want to use...
|
4
|
+
begin
|
5
|
+
require 'rubygems'
|
6
|
+
require 'active_support'
|
7
|
+
rescue LoadError => e
|
8
|
+
# These are ActiveSupport-;like extensions to do a few handy things in the gems
|
9
|
+
# Derived from ActiveSupport, so the AS copyright notice applies:
|
10
|
+
#
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# Copyright (c) 2005 David Heinemeier Hansson
|
14
|
+
#
|
15
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
16
|
+
# a copy of this software and associated documentation files (the
|
17
|
+
# "Software"), to deal in the Software without restriction, including
|
18
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
19
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
20
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
21
|
+
# the following conditions:
|
22
|
+
#
|
23
|
+
# The above copyright notice and this permission notice shall be
|
24
|
+
# included in all copies or substantial portions of the Software.
|
25
|
+
#
|
26
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
27
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
28
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
29
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
30
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
31
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
32
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
33
|
+
#++
|
34
|
+
#
|
35
|
+
#
|
36
|
+
class String #:nodoc:
|
37
|
+
|
38
|
+
# Constantize tries to find a declared constant with the name specified
|
39
|
+
# in the string. It raises a NameError when the name is not in CamelCase
|
40
|
+
# or is not initialized.
|
41
|
+
#
|
42
|
+
# Examples
|
43
|
+
# "Module".constantize #=> Module
|
44
|
+
# "Class".constantize #=> Class
|
45
|
+
def constantize()
|
46
|
+
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
|
47
|
+
raise NameError, "#{self.inspect} is not a valid constant name!"
|
48
|
+
end
|
49
|
+
|
50
|
+
Object.module_eval("::#{$1}", __FILE__, __LINE__)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
class Object #:nodoc:
|
57
|
+
# "", " ", nil, [], and {} are blank
|
58
|
+
def blank?
|
59
|
+
if respond_to?(:empty?) && respond_to?(:strip)
|
60
|
+
empty? or strip.empty?
|
61
|
+
elsif respond_to?(:empty?)
|
62
|
+
empty?
|
63
|
+
else
|
64
|
+
!self
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class NilClass #:nodoc:
|
70
|
+
def blank?
|
71
|
+
true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class FalseClass #:nodoc:
|
76
|
+
def blank?
|
77
|
+
true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class TrueClass #:nodoc:
|
82
|
+
def blank?
|
83
|
+
false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class Array #:nodoc:
|
88
|
+
alias_method :blank?, :empty?
|
89
|
+
end
|
90
|
+
|
91
|
+
class Hash #:nodoc:
|
92
|
+
alias_method :blank?, :empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
class String #:nodoc:
|
96
|
+
def blank?
|
97
|
+
empty? || strip.empty?
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class Numeric #:nodoc:
|
102
|
+
def blank?
|
103
|
+
false
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|