trydefault 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/CHANGELOG +3 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +29 -0
  4. data/lib/try_default/default.rb +58 -0
  5. metadata +57 -0
@@ -0,0 +1,3 @@
1
+ Changes in version 0.1.0 (2009-03-20)
2
+ -------------------------------------
3
+ initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ PKG_NAME = "trydefault"
4
+ PKG_VERSION = "0.1.0"
5
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
6
+ PKG_FILES = FileList[
7
+ '[A-Z]*',
8
+ 'lib/**/*'
9
+ ]
10
+ spec = Gem::Specification.new do |s|
11
+ s.platform = Gem::Platform::RUBY
12
+ s.summary = "Avoid nil or missing method exception in a call"
13
+ s.name = PKG_NAME
14
+ s.version = PKG_VERSION
15
+ s.require_path = 'lib'
16
+ s.homepage = %q{http://rinter.rubyforge.org/}
17
+ s.rubyforge_project = 'Try Default'
18
+ s.has_rdoc = false
19
+ s.authors = ["Leon Li"]
20
+ s.email = "scorpio_leon@hotmail.com"
21
+ s.files = PKG_FILES
22
+ s.description = <<-EOF
23
+ Avoid nil or missing method exception in a call
24
+ EOF
25
+ end
26
+ Rake::GemPackageTask.new(spec) do |pkg|
27
+ pkg.need_zip = true
28
+ pkg.need_tar = true
29
+ end
@@ -0,0 +1,58 @@
1
+ class Object
2
+ def try(target=self) #alias
3
+ TryDefault::Handler.new(target)
4
+ end
5
+ alias_method :try_for, :try unless method_defined?(:try_for)
6
+ end
7
+ class NilClass
8
+ undef :try
9
+ end
10
+ module TryDefault
11
+ class Handler
12
+ instance_methods.each { |m| undef_method m unless m =~ /^__/ }
13
+ def initialize(proxy=nil)
14
+ @proxy = proxy
15
+ end
16
+ def method_missing(sym, *args)
17
+ @proxy = @proxy.send(sym, *args) rescue nil
18
+ self
19
+ end
20
+ def default(value = '')
21
+ @proxy ||= value
22
+ end
23
+ def to_s
24
+ @proxy.to_s
25
+ end
26
+ def to_str
27
+ to_s
28
+ end
29
+ def inspect
30
+ @proxy.inspect
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ if __FILE__ == $0
37
+ #for method
38
+ def xxx
39
+ "yes"
40
+ end
41
+ p try.xxx == 'yes'
42
+ p (try.xxx.default "no") == 'yes'
43
+ p try.xxx.yyy.zzz.nil? == true
44
+ #p (try.xxx.yyy.zzz || "no") == 'no' will fails, because try.xxx.yyy.zzz is a proxy object
45
+ p (try.xxx.yyy.zzz.default "no") == 'no'
46
+ #for variable
47
+ @xxx = "yes"
48
+ p try(@xxx) == 'yes'
49
+ p (try(@xxx).default "no") == 'yes'
50
+ p try(@xxx).yyy.zzz.nil? == true
51
+ p (try(@xxx).yyy.zzz.default "no") == 'no'
52
+ p @xxx.try.yyy.zzz.nil? == true
53
+ p "#{try.xxx}" == 'yes'
54
+ p "#{try.xxx.yyy.zzz}" == ''
55
+
56
+ #That will throw undefined method error for makr sure yyy not nil
57
+ #p xxx.yyy.try.zzz
58
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trydefault
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leon Li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-23 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Avoid nil or missing method exception in a call
17
+ email: scorpio_leon@hotmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - Rakefile
27
+ - CHANGELOG
28
+ - lib/try_default
29
+ - lib/try_default/default.rb
30
+ has_rdoc: false
31
+ homepage: http://rinter.rubyforge.org/
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project: Try Default
52
+ rubygems_version: 1.3.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: Avoid nil or missing method exception in a call
56
+ test_files: []
57
+