idgas 0.0.1
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.rdoc +26 -0
- data/lib/i_dont_give_a_shit.rb +37 -0
- data/test/test_i_dont_give_a_shit.rb +53 -0
- metadata +58 -0
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= I dont give a shit (idgas)
|
2
|
+
|
3
|
+
Inspired by groovys questionmark-syntax. 'I dont give a shit' tries to implement this behavior in ruby.
|
4
|
+
|
5
|
+
You can put a ? behind every method-call to ignore, weather it returns nil.
|
6
|
+
|
7
|
+
So chaining of calls within deep data-structures is painless:
|
8
|
+
|
9
|
+
some_data_that_might_contain_nil.order?.shoppingcart?.item?.second?.price?
|
10
|
+
|
11
|
+
This call would return the price of the second item in the shopping-cart or +nil+ if some of the objects in the data-structure is +nil+ itself.
|
12
|
+
|
13
|
+
== Install
|
14
|
+
|
15
|
+
$ gem install phoet-idgas -s http://gems.github.com
|
16
|
+
|
17
|
+
== How to use
|
18
|
+
|
19
|
+
Just require the gem and put it in a place where it can override the default behavior of your script.
|
20
|
+
You might put it as an initializer in your rails application or something like that.
|
21
|
+
|
22
|
+
$ irb -rubygems
|
23
|
+
require 'i_dont_give_a_shit'
|
24
|
+
|
25
|
+
nil.i?.dont?.give?.a?.shit?
|
26
|
+
=> nil
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Open up Object to add handling of +?+.
|
2
|
+
class Object
|
3
|
+
|
4
|
+
# Save a reference to original +method_missing+.
|
5
|
+
alias m_m method_missing
|
6
|
+
|
7
|
+
# Catches all calls that were intended to go to the real object ending with +?+.
|
8
|
+
# The question-mark is stripped and the call is processed again.
|
9
|
+
# Calls without a question-mark are send to the original +method_missing+.
|
10
|
+
def method_missing(sym,*args, &block)
|
11
|
+
method_name = sym.to_s
|
12
|
+
if /.+\?$/ =~ method_name
|
13
|
+
puts "object '#{sym}' '#{args}' '#{block}'"
|
14
|
+
send(method_name[0..-2], *args, &block)
|
15
|
+
else
|
16
|
+
puts "object missing '#{sym}' '#{args}' '#{block}'"
|
17
|
+
m_m(sym,*args, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Open up Nil to add handling of +?+.
|
23
|
+
class NilClass
|
24
|
+
|
25
|
+
# Catches all calls that were put on nil references ending with +?+ returning nil again.
|
26
|
+
# Calls without a question-mark are send to original +method_missing+.
|
27
|
+
def method_missing(sym,*args, &block)
|
28
|
+
method_name = sym.to_s
|
29
|
+
if /.+\?$/ =~ method_name
|
30
|
+
puts "nil '#{sym}' '#{args}' '#{block}'"
|
31
|
+
self
|
32
|
+
else
|
33
|
+
puts "nil missing '#{sym}' '#{args}' '#{block}'"
|
34
|
+
super.m_m(sym,*args, &block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','..','lib')
|
2
|
+
|
3
|
+
require "test/unit"
|
4
|
+
require "i_dont_give_a_shit"
|
5
|
+
|
6
|
+
class TestIDontGiveAShit < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def test_some_wired_stuff
|
9
|
+
assert_equal(nil.send(:' ?'), nil)
|
10
|
+
assert_equal(nil.blow?(nil).up?(nil, nil){}.now?(0,1,3), nil)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_nil_check_still_works
|
14
|
+
assert_equal(nil.nil?, true)
|
15
|
+
assert_equal(''.nil?, false)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_method_with_questionmark_does_not_blow_up
|
19
|
+
assert_raise(NoMethodError) { nil.blowup }
|
20
|
+
nil.blowup?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_chained_method_with_questionmark_does_not_blow_up
|
24
|
+
assert_raise(NoMethodError) { nil.blowup?.up?.now }
|
25
|
+
nil.blowup?.up?.now?
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_call_for_tosomething_works
|
29
|
+
assert_equal("", nil.to_s)
|
30
|
+
assert_equal('', nil.is_it_empty?.to_s)
|
31
|
+
assert_equal(0, nil.is_it_zeor?.to_i)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_call_with_block_success_does_not_blow_up
|
35
|
+
nil.some?.block?{puts 'hi'}
|
36
|
+
nil.some?('a','b','c').block?{puts 'hi'}
|
37
|
+
end
|
38
|
+
|
39
|
+
class Blow
|
40
|
+
def up
|
41
|
+
'NOT!'
|
42
|
+
end
|
43
|
+
def not
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_call_for_existing_methods_works
|
49
|
+
assert_equal('NOT!', Blow.new.up?.to_s)
|
50
|
+
assert_equal('', Blow.new?.not?.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: idgas
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Peter Schr\xC3\xB6der"
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-05 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Helper for handling nil-calls.
|
17
|
+
email: phoetmail@googlemail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- lib/i_dont_give_a_shit.rb
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/phoet/idgas
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- -a
|
34
|
+
- --inline-source
|
35
|
+
- --charset=UTF-8
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project: none
|
53
|
+
rubygems_version: 1.3.5
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: If you just dont give a shit about nil add a questionmark to your method-call.
|
57
|
+
test_files:
|
58
|
+
- test/test_i_dont_give_a_shit.rb
|