quick_attr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/quick_attr.rb +56 -0
- data/test/test_quick_attr.rb +146 -0
- metadata +78 -0
data/lib/quick_attr.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module QuickAttr
|
2
|
+
#QUICK_ATTR_NIL = "QUICK_ATTR_NIL".freeze
|
3
|
+
def quick_attr (*args)
|
4
|
+
args.each {|var|
|
5
|
+
ivar = "@#{var}"
|
6
|
+
# send :define_method, var, proc {|arg|
|
7
|
+
# return send (:instance_variable_get, ivar) if arg.nil?
|
8
|
+
# send (:instance_variable_set, ivar, arg)
|
9
|
+
# self
|
10
|
+
# }
|
11
|
+
class_eval "def #{var} (*arg,&block)
|
12
|
+
raise \"cannot call #{var} with block and (#{args.join(',')}\" if arg.length > 0 and block
|
13
|
+
arg[0] = block if block
|
14
|
+
return #{ivar} if arg.length == 0
|
15
|
+
#{ivar} = arg[0] if arg.length == 1
|
16
|
+
#{ivar} = arg if arg.length > 1
|
17
|
+
self
|
18
|
+
end"
|
19
|
+
|
20
|
+
#class_eval "def #{var} (arg=(QUICK_ATTR_NIL = \"\"))
|
21
|
+
# return #{ivar} if arg.object_id == QUICK_ATTR_NIL.object_id
|
22
|
+
# #{ivar} = arg
|
23
|
+
# self
|
24
|
+
#end"
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def quick_klass (*args)
|
29
|
+
unless args.first.is_a? Class then
|
30
|
+
raise "quick_klass's first argument must be a class, was #{args.first}, try Hash, or Array for example"
|
31
|
+
end
|
32
|
+
klass = args.shift
|
33
|
+
args.each {|var|
|
34
|
+
ivar = "@#{var}"
|
35
|
+
class_eval "def #{var} (*arg)
|
36
|
+
if arg.length == 0 then
|
37
|
+
return #{ivar} = #{ivar} || #{klass.name}.new
|
38
|
+
else
|
39
|
+
#{ivar} = arg.is_a?(#{klass}) ? arg : #{klass}.new(arg)
|
40
|
+
self
|
41
|
+
end
|
42
|
+
end"
|
43
|
+
|
44
|
+
}
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
def quick_array (*args)
|
49
|
+
quick_klass(Array,*args)
|
50
|
+
end
|
51
|
+
def quick_hash (*args)
|
52
|
+
quick_klass(Hash,*args)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'lib/quick_attr'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestQuickAttr < Test::Unit::TestCase
|
5
|
+
include Test::Unit
|
6
|
+
|
7
|
+
class Hello
|
8
|
+
extend QuickAttr
|
9
|
+
#include QuickAttr
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_simple
|
13
|
+
Hello.quick_attr :one,:two,:three
|
14
|
+
|
15
|
+
h = Hello.new.one("A").two("B").three("C")
|
16
|
+
assert_equal "A", h.one
|
17
|
+
assert_equal "B", h.two
|
18
|
+
assert_equal "C", h.three
|
19
|
+
|
20
|
+
assert_equal -1,h.method(:one).arity
|
21
|
+
assert_equal -1,h.method(:two).arity
|
22
|
+
assert_equal -1,h.method(:three).arity
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def test_simple2
|
27
|
+
Hello.quick_attr :one,:two,:three
|
28
|
+
h = Hello.new.one(100).two(200).three(300)
|
29
|
+
assert_equal 100, h.one
|
30
|
+
assert_equal 200, h.two
|
31
|
+
assert_equal 300, h.three
|
32
|
+
|
33
|
+
assert_equal -1,h.method(:one).arity
|
34
|
+
assert_equal -1,h.method(:two).arity
|
35
|
+
assert_equal -1,h.method(:three).arity
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_nil_or_empty
|
39
|
+
Hello.quick_attr :_nil,:_empty,:_false
|
40
|
+
h = Hello.new._nil(nil)._empty("")._false(false)
|
41
|
+
assert_equal nil, h._nil
|
42
|
+
assert_equal "", h._empty
|
43
|
+
assert_equal false, h._false
|
44
|
+
|
45
|
+
assert_equal -1,h.method(:_nil).arity
|
46
|
+
assert_equal -1,h.method(:_empty).arity
|
47
|
+
assert_equal -1,h.method(:_false).arity
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_arrays
|
51
|
+
Hello.quick_attr :one,:two,:three
|
52
|
+
h = Hello.new.one([]).two(1,2,3,4,5).three(nil)
|
53
|
+
|
54
|
+
assert_equal [], h.one
|
55
|
+
assert_equal [1,2,3,4,5], h.two
|
56
|
+
assert_equal nil, h.three
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_proc
|
60
|
+
Hello.quick_attr :one,:two
|
61
|
+
h = Hello.new.one{true}.two(proc {3})
|
62
|
+
assert_equal true, h.one.call
|
63
|
+
assert_equal 3, h.two.call
|
64
|
+
begin
|
65
|
+
h.one ("args") {"fashl"}
|
66
|
+
fail "set either a block or a value... not both"
|
67
|
+
rescue; end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_class_space
|
71
|
+
Hello.quick_attr :one,:two
|
72
|
+
h = Hello.new.one(100).two(200)
|
73
|
+
puts h.instance_eval( "one + two")
|
74
|
+
assert_equal 300, h.instance_eval( "one + two")
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class Hello2
|
79
|
+
extend QuickAttr
|
80
|
+
quick_attr :one,:two
|
81
|
+
quick_array :several
|
82
|
+
def three
|
83
|
+
one + two
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def dont_test_class_space2
|
88
|
+
|
89
|
+
h = Hello2.new.one(111).two(222)
|
90
|
+
puts h.three
|
91
|
+
assert_equal 333,h.three
|
92
|
+
one = 10
|
93
|
+
two = 10
|
94
|
+
three = 10
|
95
|
+
|
96
|
+
b = h.instance_eval("proc do one + two + three end")
|
97
|
+
assert_equal 666, b.call
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_quick_array
|
101
|
+
h = Hello2.new.several(:a,:b)
|
102
|
+
assert_equal [:a,:b],h.several
|
103
|
+
h.several(:a)
|
104
|
+
assert_equal [:a],h.several
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_quick_array2
|
108
|
+
h = Hello2.new
|
109
|
+
h.several << :a
|
110
|
+
h.several << :b
|
111
|
+
|
112
|
+
assert_equal [:a,:b],h.several
|
113
|
+
h.several(:a)
|
114
|
+
assert_equal [:a],h.several
|
115
|
+
end
|
116
|
+
|
117
|
+
class NewArray < Array
|
118
|
+
|
119
|
+
end
|
120
|
+
class Hello3
|
121
|
+
extend QuickAttr
|
122
|
+
quick_attr :one,:two
|
123
|
+
quick_klass NewArray,:several
|
124
|
+
def three
|
125
|
+
one + two
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_quick_array_with_class
|
130
|
+
h = Hello3.new
|
131
|
+
h.several << :a
|
132
|
+
h.several << :b
|
133
|
+
|
134
|
+
assert_equal [:a,:b],h.several
|
135
|
+
assert h.several.is_a?(NewArray), "Expected NewArray"
|
136
|
+
h.several(:a)
|
137
|
+
assert h.several.is_a?(NewArray), "Expected NewArray"
|
138
|
+
assert_equal [:a],h.several
|
139
|
+
assert h.several.is_a?(NewArray), "Expected NewArray"
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quick_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dominic Tarr
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-10 00:00:00 +12:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: "\t\tusage:\n\
|
23
|
+
\t\trequire 'quick_attr'\n\
|
24
|
+
\t\tclass C\n\
|
25
|
+
\t\t\textend QuickAttr\n\
|
26
|
+
\t\t\tquick_attr :name, :smell\n\
|
27
|
+
\t\tend\n\n\
|
28
|
+
\t\t#set attr by passing value\n\
|
29
|
+
\t\tc = C.new.name(\"bobaloba ding-dong\").smell(\"fishy\")\n\n\
|
30
|
+
\t\t#get attr by calling without args...\n\
|
31
|
+
\t\tputs c.name\t# bobaloba ding-dong\n\
|
32
|
+
\t\tputs c.smell\t# fishy\n"
|
33
|
+
email: dominic.tarr@gmail.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
files:
|
41
|
+
- lib/quick_attr.rb
|
42
|
+
- test/test_quick_attr.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/dominictarr/quick_attr
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: chain attr methods
|
77
|
+
test_files: []
|
78
|
+
|