hobosupport 0.9.0 → 0.9.100

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'activerecord'
2
2
  ActiveRecord::ActiveRecordError # hack for https://rails.lighthouseapp.com/projects/8994/tickets/2577-when-using-activerecordassociations-outside-of-rails-a-nameerror-is-thrown
3
3
  require File.dirname(__FILE__) + '/lib/hobo_support.rb'
4
4
 
5
- RUBY = ENV['RUBY'] || (defined?(JRUBY_VERSION) ? 'jruby' : 'ruby')
5
+ RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).sub(/.*\s.*/m, '"\&"')
6
6
  RUBYDOCTEST = ENV['RUBYDOCTEST'] || "#{RUBY} `which rubydoctest`"
7
7
 
8
8
  namespace "test" do
@@ -1,10 +1,11 @@
1
1
  # Define BlankSlate in case ActiveSupport aint present
2
2
  unless defined? BlankSlate
3
3
  class BlankSlate
4
- instance_methods.reject { |m| m =~ /^__/ }.each { |m| undef_method m }
4
+ instance_methods.reject { |m| m =~ /^__/ || m.to_s == 'object_id' }.each { |m| undef_method m }
5
5
  def initialize(me)
6
6
  @me = me
7
7
  end
8
8
  end
9
9
  end
10
10
 
11
+
@@ -35,7 +35,7 @@ module Enumerable
35
35
 
36
36
  class MultiSender
37
37
 
38
- undef_method(*(instance_methods - %w*__id__ __send__*))
38
+ undef_method(*(instance_methods.map{|m| m.to_s} - %w*__id__ __send__ object_id*))
39
39
 
40
40
  def initialize(enumerable, method)
41
41
  @enumerable = enumerable
@@ -10,7 +10,7 @@ class Module
10
10
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
11
11
  yield(aliased_target, punctuation) if block_given?
12
12
  without = "#{aliased_target}_without_#{feature}#{punctuation}"
13
- unless instance_methods.include?(without)
13
+ unless method_defined?(without)
14
14
  alias_method without, target
15
15
  alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
16
16
  end
@@ -65,6 +65,9 @@ class Hash
65
65
  keys.each { |k| delete(k) if self[k].nil? }
66
66
  end
67
67
 
68
+ # Ruby 1.9.1 complains about the use of index and recommends key
69
+ # but Ruby 1.8 doesn't have key. Add it.
70
+ alias_method(:key, :index) unless method_defined?(:key)
68
71
 
69
72
  end
70
73
 
@@ -56,9 +56,32 @@ end
56
56
  class SafeNil
57
57
  include Singleton
58
58
 
59
+ DONT_REDEFINE_METHODS = "__id__", "__send__", "object_id"
60
+
61
+ NIL_RESPONSE_METHODS = ["to_s", "to_json", "to_yaml", "__id__", "__is_a__", "__metaclass__", "__send__"]
62
+
63
+ (NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
64
+ # can't use define_method with a block
65
+ eval "
66
+ def #{method}(*args, &b)
67
+ nil.send(:#{method}, *args, &b)
68
+ end"
69
+ end
70
+
71
+ (instance_methods.map{|m| m.to_s} - NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
72
+ # can't use define_method with a block
73
+ eval "
74
+ def #{method}(*args, &b)
75
+ nil
76
+ end"
77
+ end
78
+
79
+ def to_s
80
+ ""
81
+ end
82
+
59
83
  def method_missing(method, *args, &b)
60
- return nil unless nil.respond_to? method
61
- nil.send(method, *args, &b) rescue nil
84
+ return nil
62
85
  end
63
86
  end
64
87
 
@@ -9,7 +9,9 @@ module Kernel
9
9
 
10
10
  end
11
11
 
12
- class It < BlankSlate
12
+ class It
13
+
14
+ instance_methods.reject { |m| m =~ /^__/ || m.to_s == 'object_id' }.each { |m| undef_method m }
13
15
 
14
16
  def initialize
15
17
  @methods = []
data/lib/hobo_support.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module HoboSupport
2
2
 
3
- VERSION = "0.9.0"
3
+ VERSION = "0.9.100"
4
4
 
5
5
  RAILS_VERSION_FLOAT = Object.const_defined?(:Rails) ? Rails::VERSION::STRING.match(/^\d+\.\d+/)[0].to_f : 0
6
6
 
@@ -7,14 +7,14 @@ Why the Luck Stiff's essential meta-programming additions to Object. These are p
7
7
 
8
8
  ## `Object#metaclass`
9
9
 
10
- Returns the "metaclass" (bad name) or "singleton class" of a given ruby Object
10
+ Returns the "metaclass", "eigenclass" or "singleton class" of a given ruby Object
11
11
 
12
12
  >> o = Object.new
13
13
  >> def o.foo; ;123; end
14
14
  >> o.foo
15
15
  => 123
16
- >> o.metaclass.instance_methods.grep "foo"
17
- => ["foo"]
16
+ >> o.metaclass.method_defined?(:foo)
17
+ => true
18
18
 
19
19
  ## `Object#meta_eval`
20
20
 
@@ -8,7 +8,7 @@ HoboSupport is a mixed bag of core ruby extensions that have been extracted from
8
8
  {.hidden}
9
9
 
10
10
  >> HoboSupport::VERSION
11
- => "0.9.0"
11
+ => "0.9.100"
12
12
 
13
13
  ## Contents
14
14
 
@@ -34,23 +34,39 @@ Like `is_a?` but multiple types to be checked in one go
34
34
 
35
35
  ## Method call extensions
36
36
 
37
+ We have the "." operator to call methods on objects. These extensions introduce two "special dots".
38
+
37
39
  ### `Object#_?`
38
40
 
39
- We have the "." operator to call methods on objects. These extensions introduce two "special dots". "`._?.`" only calls the method if the receiver is not `nil`.
41
+ "`._?.`" only calls the method if the receiver is not `nil`. Otherwise it returns nil.
40
42
 
41
43
  >> "foo"._?.length
42
44
  => 3
43
45
  >> nil._?.length
44
46
  => nil
45
47
 
48
+ `to_s`, `to_yaml` and `to_json` and a few system functions that start with an underscore are the only exceptions -- they call their corresponding functions on nil.
49
+
50
+ >> nil._?.to_s
51
+ => ""
46
52
 
47
53
  ### `Object#try`
48
54
 
49
55
  "`.try`" only calls the method if the receiver responds to that method.
50
56
 
51
- >> "foo".try.length
52
- => 3
53
- >> :foo.try.length
57
+ >> "foo".try.reverse
58
+ => "oof"
59
+ >> :foo.try.reverse
54
60
  => nil
55
61
 
62
+ ### What's the difference?
56
63
 
64
+ `_?` is generally used when nil is an expected response. `try` is
65
+ generally used when interfaces may be different. For instance, you
66
+ may use `try` to call a Rails 2.3 function that doesn't exist on Rails
67
+ 2.2. nil responds to some functions that may surprise you.
68
+
69
+ >> nil.try.to_i
70
+ => 0
71
+ >> nil._?.to_i
72
+ => nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobosupport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.100
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Locke
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-17 00:00:00 +00:00
12
+ date: 2009-12-01 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15