hobosupport 0.9.103 → 0.9.104

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,16 +3,17 @@ ActiveRecord::ActiveRecordError # hack for https://rails.lighthouseapp.com/proje
3
3
  require File.dirname(__FILE__) + '/lib/hobo_support.rb'
4
4
 
5
5
  RUBY = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']).sub(/.*\s.*/m, '"\&"')
6
- RUBYDOCTEST = ENV['RUBYDOCTEST'] || "#{RUBY} `which rubydoctest`"
6
+ RUBYDOCTEST = ENV['RUBYDOCTEST'] || "#{RUBY} -S rubydoctest"
7
7
 
8
8
  namespace "test" do
9
9
  desc "Run the doctests"
10
10
  task :doctest do |t|
11
- exit(1) if !system("#{RUBYDOCTEST} test/*.rdoctest test/hobosupport/*.rdoctest")
11
+ files=Dir['test/*.rdoctest','test/hobosupport/*.rdoctest'].map {|f| File.expand_path(f)}.join(' ')
12
+ exit(1) if !system("#{RUBYDOCTEST} --single #{files}")
12
13
  end
13
14
  end
14
15
 
15
- # --- build gem via Jeweller --- #
16
+ # --- build gem via Jeweler --- #
16
17
 
17
18
  require 'jeweler'
18
19
  Jeweler::Tasks.new do |gemspec|
@@ -1,9 +1,9 @@
1
1
  # The dot operator calls methods on objects. Power Dots are dots with extra features
2
2
  #
3
- # .? calls a method if the reciever is not nil, returns nil
3
+ # .? calls a method if the receiver is not nil, returns nil
4
4
  # otherwise. We have to write it ._?. in order to be valid Ruby
5
5
  #
6
- # .try. calls a mehod only if the recipient resonds to that method
6
+ # .try. calls a method only if the recipient responds to that method
7
7
 
8
8
  require 'delegate'
9
9
  require 'singleton'
@@ -56,30 +56,13 @@ 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"
59
+ begin
60
+ old_verbose, $VERBOSE = $VERBOSE, nil # just like Rails silence_warnings: suppress "warning: undefining `object_id' may cause serious problem"
61
+ instance_methods.each { |m| undef_method m unless m[0,2] == '__' }
62
+ ensure
63
+ $VERBOSE = old_verbose
77
64
  end
78
65
 
79
- def to_s
80
- ""
81
- end
82
-
83
66
  def method_missing(method, *args, &b)
84
67
  return nil
85
68
  end
@@ -120,4 +103,15 @@ module ActiveRecord
120
103
  end
121
104
  end
122
105
  end
106
+
107
+ module NamedScope
108
+ class Scope
109
+ # we need to make sure we don't trigger AssociationCollections' method_missing
110
+ def try(*args, &block)
111
+ HoboSupport.hobo_try(self, *args, &block)
112
+ end
113
+ end
114
+ end
115
+
123
116
  end
117
+
data/lib/hobo_support.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module HoboSupport
2
2
 
3
- VERSION = "0.9.103"
3
+ VERSION = "0.9.104"
4
4
 
5
5
  RAILS_VERSION_FLOAT = Object.const_defined?(:Rails) ? Rails::VERSION::STRING.match(/^\d+\.\d+/)[0].to_f : 0
6
6
 
@@ -28,7 +28,7 @@ class String
28
28
  def safe_constantize
29
29
  Object.class_eval self
30
30
  rescue NameError => e
31
- # Unforunately we have to rely on the error message to figure out which constant was missing.
31
+ # Unfortunately we have to rely on the error message to figure out which constant was missing.
32
32
  # NameError has a #name method but it is always nil
33
33
  if e.message !~ /\b#{self}$/
34
34
  # oops - some other name error
@@ -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.103"
11
+ => "0.9.104"
12
12
 
13
13
  ## Contents
14
14
 
@@ -38,21 +38,26 @@ We have the "." operator to call methods on objects. These extensions introduce
38
38
 
39
39
  ### `Object#_?`
40
40
 
41
- "`._?.`" only calls the method if the receiver is not `nil`. Otherwise it returns nil.
41
+ "`._?.`" only calls the method if the receiver is not `nil`. Otherwise it returns nil. So `x._?.method(*args)` is equivalent to `(nil == x ? nil : x.method(*args))`.
42
42
 
43
43
  >> "foo"._?.length
44
44
  => 3
45
45
  >> nil._?.length
46
46
  => nil
47
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.
48
+ When the receiver is nil, any method with any arguments will return nil. You can use Ruby's || idiom to provide a different value when nil.
49
49
 
50
- >> nil._?.to_s
51
- => ""
50
+ >> expires = nil
51
+ >> expires._?.to_s( :default ) || "never"
52
+ => "never"
53
+
54
+ Note that `_?` should *always* be followed by a method call. It is not intended to store the intermediate result. Don't do this!
55
+
56
+ intermediate = nil._?
52
57
 
53
58
  ### `Object#try`
54
59
 
55
- "`.try`" only calls the method if the receiver responds to that method.
60
+ "`.try`" only calls the method if the receiver responds to that method. Otherwise it returns nil. So `x.try.method(*args)` is equivalent to `(x.respond_to?(:method) ? x.method(*args) : nil)`.
56
61
 
57
62
  >> "foo".try.reverse
58
63
  => "oof"
@@ -61,10 +66,10 @@ We have the "." operator to call methods on objects. These extensions introduce
61
66
 
62
67
  ### What's the difference?
63
68
 
64
- `_?` is generally used when nil is an expected response. `try` is
65
- generally used when interfaces may be different. For instance, you
69
+ Use `_?` when you want to call a method but you know the receiver may be nil.
70
+ Use `try` when you want to call a method but you know the receiver may not respond to it. For instance, you
66
71
  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.
72
+ 2.2. Note that nil responds to some functions that may surprise you.
68
73
 
69
74
  >> nil.try.to_i
70
75
  => 0
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.103
4
+ version: 0.9.104
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-12-10 00:00:00 -05:00
12
+ date: 2010-01-21 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15