tinyext 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,9 @@
1
+ Adds the following core extensions:
2
+
3
+ Object#blank? and #present? (taken from ActiveSupport)
4
+ Symbol#to_proc (backport of Ruby 1.8.7 feature)
5
+ Object#tap (backport of Ruby 1.8.7 feature)
6
+ Object#try (taken from ActiveSupport)
7
+
8
+ Also backports a Ruby 1.9 performance improvement to net/http, which needs to be required manually
9
+
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/test*.rb']
6
+ t.verbose = true
7
+ end
8
+ task :default => :test
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gemspec|
13
+ gemspec.name = "tinyext"
14
+ gemspec.summary = "Tiny set of core extensions. Backports of Ruby 1.9 features."
15
+ gemspec.description = "Adds Object#blank?, #present?, #tap? and Symbol#to_proc"
16
+ gemspec.email = "gbuesing@gmail.com"
17
+ gemspec.homepage = "http://github.com/gbuesing/tinyext"
18
+ gemspec.authors = ["Geoff Buesing"]
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available. Install it with: gem install jeweler"
22
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/tinyext.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tinyext/core'
@@ -0,0 +1,4 @@
1
+ require 'tinyext/core/blank'
2
+ require 'tinyext/core/symbol'
3
+ require 'tinyext/core/tap'
4
+ require 'tinyext/core/try'
@@ -0,0 +1,59 @@
1
+ # TAKEN FROM ACTIVE SUPPORT
2
+ class Object
3
+ # An object is blank if it's false, empty, or a whitespace string.
4
+ # For example, "", " ", +nil+, [], and {} are blank.
5
+ #
6
+ # This simplifies
7
+ #
8
+ # if !address.nil? && !address.empty?
9
+ #
10
+ # to
11
+ #
12
+ # if !address.blank?
13
+ def blank?
14
+ respond_to?(:empty?) ? empty? : !self
15
+ end
16
+
17
+ # An object is present if it's not blank.
18
+ def present?
19
+ !blank?
20
+ end
21
+ end
22
+
23
+ class NilClass #:nodoc:
24
+ def blank?
25
+ true
26
+ end
27
+ end
28
+
29
+ class FalseClass #:nodoc:
30
+ def blank?
31
+ true
32
+ end
33
+ end
34
+
35
+ class TrueClass #:nodoc:
36
+ def blank?
37
+ false
38
+ end
39
+ end
40
+
41
+ class Array #:nodoc:
42
+ alias_method :blank?, :empty?
43
+ end
44
+
45
+ class Hash #:nodoc:
46
+ alias_method :blank?, :empty?
47
+ end
48
+
49
+ class String #:nodoc:
50
+ def blank?
51
+ self !~ /\S/
52
+ end
53
+ end
54
+
55
+ class Numeric #:nodoc:
56
+ def blank?
57
+ false
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ # TAKEN FROM ACTIVE SUPPORT
2
+ unless :to_proc.respond_to?(:to_proc)
3
+ class Symbol
4
+ # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
5
+ #
6
+ # # The same as people.collect { |p| p.name }
7
+ # people.collect(&:name)
8
+ #
9
+ # # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
10
+ # people.select(&:manager?).collect(&:salary)
11
+ def to_proc
12
+ Proc.new { |*args| args.shift.__send__(self, *args) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ # TAKEN FROM ACTIVE SUPPORT
2
+ class Object
3
+ # Yields <code>x</code> to the block, and then returns <code>x</code>.
4
+ # The primary purpose of this method is to "tap into" a method chain,
5
+ # in order to perform operations on intermediate results within the chain.
6
+ #
7
+ # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
8
+ # tap { |x| puts "array: #{x.inspect}" }.
9
+ # select { |x| x%2 == 0 }.
10
+ # tap { |x| puts "evens: #{x.inspect}" }.
11
+ # map { |x| x*x }.
12
+ # tap { |x| puts "squares: #{x.inspect}" }
13
+ def tap
14
+ yield self
15
+ self
16
+ end unless Object.respond_to?(:tap)
17
+ end
@@ -0,0 +1,37 @@
1
+ # TAKEN FROM ACTIVE SUPPORT
2
+ class Object
3
+ # Invokes the method identified by the symbol +method+, passing it any arguments
4
+ # and/or the block specified, just like the regular Ruby <tt>Object#send</tt> does.
5
+ #
6
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
7
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
8
+ #
9
+ # ==== Examples
10
+ #
11
+ # Without try
12
+ # @person && @person.name
13
+ # or
14
+ # @person ? @person.name : nil
15
+ #
16
+ # With try
17
+ # @person.try(:name)
18
+ #
19
+ # +try+ also accepts arguments and/or a block, for the method it is trying
20
+ # Person.try(:find, 1)
21
+ # @people.try(:collect) {|p| p.name}
22
+ #--
23
+ # This method definition below is for rdoc purposes only. The alias_method call
24
+ # below overrides it as an optimization since +try+ behaves like +Object#send+,
25
+ # unless called on +NilClass+.
26
+ def try(method, *args, &block)
27
+ send(method, *args, &block)
28
+ end
29
+ remove_method :try
30
+ alias_method :try, :__send__
31
+ end
32
+
33
+ class NilClass
34
+ def try(*args)
35
+ nil
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ # Monkey patch for faster net/http io
2
+ # Backport of 1.9 improvement
3
+ if RUBY_VERSION.to_f < 1.9
4
+ class Net::BufferedIO #:nodoc:
5
+ alias :old_rbuf_fill :rbuf_fill
6
+ def rbuf_fill
7
+ if @io.respond_to?(:read_nonblock)
8
+ begin
9
+ @rbuf << @io.read_nonblock(65536)
10
+ rescue Errno::EWOULDBLOCK
11
+ if IO.select([@io], nil, nil, @read_timeout)
12
+ retry
13
+ else
14
+ raise Timeout::Error
15
+ end
16
+ end
17
+ else
18
+ timeout(@read_timeout) do
19
+ @rbuf << @io.sysread(65536)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'tinyext'
3
+
4
+ class TestTinyext < Test::Unit::TestCase
5
+
6
+ def test_object_blank
7
+ assert_equal true, [].blank?
8
+ assert_equal false, [1].blank?
9
+ end
10
+
11
+ def test_object_present
12
+ assert_equal false, [].present?
13
+ assert_equal true, [1].present?
14
+ end
15
+
16
+ def test_symbol_to_proc
17
+ assert_equal ['1','2','3'], [1,2,3].map(&:to_s)
18
+ end
19
+
20
+ def test_object_tap
21
+ 'foo'.tap do |s|
22
+ assert_equal 'foo', s
23
+ end
24
+ end
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinyext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Geoff Buesing
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-12 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "Adds Object#blank?, #present?, #tap? and Symbol#to_proc"
17
+ email: gbuesing@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/tinyext.rb
29
+ - lib/tinyext/core.rb
30
+ - lib/tinyext/core/blank.rb
31
+ - lib/tinyext/core/symbol.rb
32
+ - lib/tinyext/core/tap.rb
33
+ - lib/tinyext/core/try.rb
34
+ - lib/tinyext/net/http.rb
35
+ - test/test_tinyext.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/gbuesing/tinyext
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Tiny set of core extensions. Backports of Ruby 1.9 features.
64
+ test_files:
65
+ - test/test_tinyext.rb