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 +1 -1
- data/lib/hobo_support/blankslate.rb +2 -1
- data/lib/hobo_support/enumerable.rb +1 -1
- data/lib/hobo_support/fixes/module.rb +1 -1
- data/lib/hobo_support/hash.rb +3 -0
- data/lib/hobo_support/methodcall.rb +25 -2
- data/lib/hobo_support/methodphitamine.rb +3 -1
- data/lib/hobo_support.rb +1 -1
- data/test/hobosupport/metaid.rdoctest +3 -3
- data/test/hobosupport.rdoctest +21 -5
- metadata +2 -2
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 =
|
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
|
+
|
@@ -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
|
13
|
+
unless method_defined?(without)
|
14
14
|
alias_method without, target
|
15
15
|
alias_method target, "#{aliased_target}_with_#{feature}#{punctuation}"
|
16
16
|
end
|
data/lib/hobo_support/hash.rb
CHANGED
@@ -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
|
61
|
-
nil.send(method, *args, &b) rescue nil
|
84
|
+
return nil
|
62
85
|
end
|
63
86
|
end
|
64
87
|
|
data/lib/hobo_support.rb
CHANGED
@@ -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"
|
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.
|
17
|
-
=>
|
16
|
+
>> o.metaclass.method_defined?(:foo)
|
17
|
+
=> true
|
18
18
|
|
19
19
|
## `Object#meta_eval`
|
20
20
|
|
data/test/hobosupport.rdoctest
CHANGED
@@ -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.
|
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
|
-
|
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.
|
52
|
-
=>
|
53
|
-
>> :foo.try.
|
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.
|
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-
|
12
|
+
date: 2009-12-01 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|