ducktypechecker 0.1.0 → 0.1.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 +1 -1
- data/lib/duck_type_checker.rb +80 -7
- data/lib/ducktypechecker.rb +1 -1
- metadata +3 -3
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Duck Type Checker is copyrighted free software by Brian Takita <
|
1
|
+
Duck Type Checker is copyrighted free software by Brian Takita <Brian.Takita@gmail.com>.
|
2
2
|
You can redistribute it and/or modify it under either the terms of the GPL, or the conditions below:
|
3
3
|
|
4
4
|
1. You may make and give away verbatim copies of the source form of the
|
data/lib/duck_type_checker.rb
CHANGED
@@ -1,12 +1,76 @@
|
|
1
|
+
# DuckType - A mixin that checks an object to see if it implements
|
2
|
+
# certain methods (ie "Walks like a Duck"). If the object does not
|
3
|
+
# implement a given method, an exception is raised
|
4
|
+
# or user defined action is performed.
|
5
|
+
#
|
6
|
+
# ===Example
|
7
|
+
# require 'ducktypechecker'
|
8
|
+
#
|
9
|
+
# class Test
|
10
|
+
# def a_method(a)
|
11
|
+
# DuckType.check(a, [:method_one, :method_two], 'a')
|
12
|
+
# puts a.method_one
|
13
|
+
# puts a.method_two
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# class A
|
18
|
+
# def method_one
|
19
|
+
# 'method_one'
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# t = Test.new
|
24
|
+
# t.a_method(A.new)
|
25
|
+
# # A TypeError exception is raised with the message
|
26
|
+
# #"Variable 'a' needs to implement 'method_two'."
|
27
|
+
#
|
28
|
+
# ==Passing a Block
|
29
|
+
# You can also use a custom action adding a block to the
|
30
|
+
# DuckType.check method.
|
31
|
+
#
|
32
|
+
# ===Example
|
33
|
+
# require 'ducktypechecker'
|
34
|
+
# s = Struct.new(:a)
|
35
|
+
# o = s.new(1)
|
36
|
+
# DuckType.check(o, [:a, :b]) do |s|
|
37
|
+
# print "Method '#{s.to_s}' is not implemented in object 'o'."
|
38
|
+
# end
|
39
|
+
# # "Method 'b' is not implemented in object 'o'."
|
40
|
+
#
|
41
|
+
# ==Mixin
|
42
|
+
# You can also mixin DuckType into your own class.
|
43
|
+
#
|
44
|
+
# ===Example
|
45
|
+
# require 'ducktypechecker'
|
46
|
+
# class Test
|
47
|
+
# include(DuckType)
|
48
|
+
# def a_method(a)
|
49
|
+
# duck_type_check(a, [:method_one, :method_two], 'a')
|
50
|
+
# puts a.method_one
|
51
|
+
# puts a.method_two
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# class A
|
56
|
+
# def method_one
|
57
|
+
# 'method_one'
|
58
|
+
# end
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# t = Test.new
|
62
|
+
# t.a_method(A.new)
|
63
|
+
# # A TypeError exception is raised with the message
|
64
|
+
# #"Variable 'a' needs to implement 'method_two'."
|
65
|
+
|
1
66
|
module DuckType
|
2
67
|
# Checks the given object if is responds to the given
|
3
68
|
# method signatures.
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# ==Block
|
69
|
+
# ===Arguments
|
70
|
+
# :obj = The object being tested
|
71
|
+
# :methods = A method name or enumerable of the method names.
|
72
|
+
# :var_name = The name of the variable begin tested.
|
73
|
+
# ===Block
|
10
74
|
# Optionally a block may be passed to respond to a method that
|
11
75
|
# is not implemented by the object.
|
12
76
|
def self.check(obj, methods, var_name = nil, &block)
|
@@ -28,7 +92,16 @@ module DuckType
|
|
28
92
|
end
|
29
93
|
end
|
30
94
|
|
31
|
-
# Instance implementation of
|
95
|
+
# Instance implementation of DuckType.check
|
96
|
+
# Checks the given object if is responds to the given
|
97
|
+
# method signatures.
|
98
|
+
# ===Arguments
|
99
|
+
# :obj = The object being tested
|
100
|
+
# :methods = A method name or enumerable of the method names.
|
101
|
+
# :var_name = The name of the variable begin tested.
|
102
|
+
# ===Block
|
103
|
+
# Optionally a block may be passed to respond to a method that
|
104
|
+
# is not implemented by the object.
|
32
105
|
def duck_type_check(obj, methods, var_name = nil, &block)
|
33
106
|
DuckType.check(obj, methods, var_name, &block)
|
34
107
|
end
|
data/lib/ducktypechecker.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
dir = File.dirname(__FILE__)
|
2
|
-
require "#{dir}/duck_type_checker"
|
2
|
+
require "#{dir}/duck_type_checker"
|
metadata
CHANGED
@@ -3,9 +3,9 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ducktypechecker
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2005-09-
|
8
|
-
summary: Check to see if an object
|
6
|
+
version: 0.1.1
|
7
|
+
date: 2005-09-05 00:00:00 -07:00
|
8
|
+
summary: Check to see if an object "walks like a duck".
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: brian.takita@gmail.com
|