arrayclass 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrayclass.rb +35 -19
- data/specs/arrayclass_spec.rb +156 -4
- data/specs/light_class_shared.rb +0 -151
- metadata +4 -4
data/lib/arrayclass.rb
CHANGED
@@ -34,15 +34,33 @@ end
|
|
34
34
|
module Arrayclass
|
35
35
|
def self.new(fields)
|
36
36
|
nclass = Class.new(Array)
|
37
|
+
nclass.class_eval('def self.inherited(sc)
|
38
|
+
sc.instance_variable_set("@arr_size", @arr_size)
|
39
|
+
sc.instance_variable_set("@ind", @ind.dup)
|
40
|
+
sc.instance_variable_set("@attributes", @attributes.dup)
|
41
|
+
end
|
42
|
+
|
43
|
+
')
|
44
|
+
|
45
|
+
nclass.class_eval('def self.size()
|
46
|
+
@arr_size
|
47
|
+
end
|
48
|
+
def self.ind()
|
49
|
+
@ind
|
50
|
+
end
|
51
|
+
')
|
52
|
+
|
37
53
|
|
38
54
|
nclass.class_eval('def self.add_member(name)
|
39
|
-
i =
|
55
|
+
i = @arr_size
|
40
56
|
self.class_eval("def #{name}() self[#{i}] end")
|
41
57
|
self.class_eval("def #{name}=(val) self[#{i}]=val end")
|
42
|
-
self.class_eval("
|
43
|
-
self.class_eval("
|
44
|
-
self.class_eval("
|
45
|
-
self.class_eval("
|
58
|
+
self.class_eval("@ind[:#{name}] = #{i}")
|
59
|
+
self.class_eval("@ind[\"#{name}\"] = #{i}")
|
60
|
+
self.class_eval("@attributes << :#{name}")
|
61
|
+
self.class_eval("@arr_size = @attributes.size")
|
62
|
+
$TMP_CRAZY = $VERBOSE; $VERBOSE = nil
|
63
|
+
$VERBOSE = $TMP_CRAZY
|
46
64
|
end')
|
47
65
|
|
48
66
|
|
@@ -53,31 +71,31 @@ module Arrayclass
|
|
53
71
|
nclass.class_eval("undef_method :#{att}")
|
54
72
|
end
|
55
73
|
nclass.class_eval("undef_method :transpose if Array.instance_methods(false).include?('transpose')")
|
56
|
-
nclass.class_eval("
|
74
|
+
nclass.class_eval("@ind = {}")
|
57
75
|
# array to preserve order
|
58
|
-
nclass.class_eval("
|
59
|
-
nclass.class_eval("
|
76
|
+
nclass.class_eval("@attributes = []")
|
77
|
+
nclass.class_eval("@arr_size = 0")
|
60
78
|
fields.each_with_index do |field,i|
|
61
79
|
nclass.add_member(field.to_s)
|
62
80
|
end
|
63
|
-
|
64
|
-
nclass.class_eval "
|
81
|
+
nclass.class_eval '
|
65
82
|
def initialize(args=nil)
|
66
83
|
if args.is_a? Array
|
67
84
|
super(args)
|
68
85
|
elsif args.is_a? Hash
|
69
|
-
super(
|
86
|
+
super(self.class.size)
|
87
|
+
h = self.class.ind
|
70
88
|
args.each do |k,v|
|
71
|
-
self[
|
89
|
+
self[h[k]] = v
|
72
90
|
end
|
73
91
|
else
|
74
|
-
super(
|
92
|
+
super(self.class.size)
|
75
93
|
end
|
76
|
-
end
|
94
|
+
end'
|
77
95
|
|
78
96
|
# list of
|
79
|
-
nclass.class_eval('def self.members()
|
80
|
-
nclass.class_eval('def members()
|
97
|
+
nclass.class_eval('def self.members() @attributes end')
|
98
|
+
nclass.class_eval('def members() self.class.members end')
|
81
99
|
nclass.class_eval("def is_a?(klass)
|
82
100
|
if klass == Array ; false
|
83
101
|
else ; super(klass)
|
@@ -86,7 +104,7 @@ module Arrayclass
|
|
86
104
|
alias_method :kind_of?, :is_a?")
|
87
105
|
|
88
106
|
nclass.class_eval('def inspect
|
89
|
-
bits =
|
107
|
+
bits = members.map do |v|
|
90
108
|
val = self.send(v)
|
91
109
|
val = "nil" if val.nil?
|
92
110
|
"#{v}=#{val.inspect}"
|
@@ -94,8 +112,6 @@ module Arrayclass
|
|
94
112
|
string = bits.join(", ")
|
95
113
|
"<(Arrayclass based) #{string}>"
|
96
114
|
end ')
|
97
|
-
#nclass.class_eval('def self.indices() @@ind end')
|
98
|
-
#nclass.class_eval('def indices() @@ind end')
|
99
115
|
|
100
116
|
# NOTE that two separate objects will hash differently (even if their
|
101
117
|
# content is the same!)
|
data/specs/arrayclass_spec.rb
CHANGED
@@ -3,11 +3,163 @@ require File.dirname(__FILE__) + '/light_class_shared'
|
|
3
3
|
|
4
4
|
require 'arrayclass'
|
5
5
|
|
6
|
+
describe Arrayclass do
|
7
|
+
before(:each) do
|
8
|
+
@generator = Arrayclass
|
9
|
+
@attrs = %w(f0 f1 f2)
|
10
|
+
@attrs_as_symbols = @attrs.map {|v| v.to_sym }
|
11
|
+
@myclass = @generator.new(@attrs)
|
12
|
+
@init_array = [0,1,2]
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'can be initialized with an array' do
|
16
|
+
obj = @myclass.new(@init_array)
|
17
|
+
obj.f0.should == obj[0]
|
18
|
+
obj.f1.should == obj[1]
|
19
|
+
obj.f2.should == obj[2]
|
20
|
+
obj.f1 = 5
|
21
|
+
obj.f1.should == 5
|
22
|
+
obj.class.should == @myclass
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'can be queried and modified by method call' do
|
26
|
+
obj = @myclass.new(@init_array)
|
27
|
+
obj.f0.should == 0
|
28
|
+
obj.f1.should == 1
|
29
|
+
obj.f2.should == 2
|
30
|
+
|
31
|
+
obj.f0 = 5
|
32
|
+
obj.f1 = 6
|
33
|
+
obj.f2 = 7
|
34
|
+
|
35
|
+
obj.f0.should == 5
|
36
|
+
obj.f1.should == 6
|
37
|
+
obj.f2.should == 7
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'can be queried and modified by array index' do
|
41
|
+
obj = @myclass.new(@init_array)
|
42
|
+
obj[0].should == 0
|
43
|
+
obj[1].should == 1
|
44
|
+
obj[2].should == 2
|
45
|
+
|
46
|
+
obj[0] = 5
|
47
|
+
obj[1] = 6
|
48
|
+
obj[2] = 7
|
49
|
+
|
50
|
+
obj[0].should == 5
|
51
|
+
obj[1].should == 6
|
52
|
+
obj[2].should == 7
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can have methods added' do
|
56
|
+
MyClass = @generator.new([:one])
|
57
|
+
class MyClass
|
58
|
+
def hello() 'hiya' end
|
59
|
+
end
|
60
|
+
obj = MyClass.new
|
61
|
+
obj.hello.should == 'hiya'
|
62
|
+
# OR -->
|
63
|
+
#@myclass.class_eval("def hello() 'hiya' end")
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'can be initialized with a hash' do
|
67
|
+
## Hash initialization:
|
68
|
+
obj = @myclass.new(:f0 => 0, :f1 => 1, :f2 => 2)
|
69
|
+
obj.f0.should == 0
|
70
|
+
obj.f1.should == 1
|
71
|
+
obj.f2.should == 2
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'can set and retrieve multiple members in a single slice' do
|
75
|
+
obj = @myclass.new
|
76
|
+
obj[0,3] = [4,5,6]
|
77
|
+
obj[0].should == 4
|
78
|
+
obj[1].should == 5
|
79
|
+
obj[2].should == 6
|
80
|
+
obj[0..-1] = [7,8,9]
|
81
|
+
obj[0].should == 7
|
82
|
+
obj[1].should == 8
|
83
|
+
obj[2].should == 9
|
84
|
+
|
85
|
+
obj[0,2].should == [7,8]
|
86
|
+
obj[0..1].should == [7,8]
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'has a member list (like Struct)' do
|
90
|
+
obj = @myclass.new
|
91
|
+
obj.members.should == @attrs_as_symbols
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'does not respond to calls that would potentially mess up your object' do
|
95
|
+
%w(flatten flatten! assoc join rassoc push pop <<).each do |cl|
|
96
|
+
obj = @myclass.new
|
97
|
+
obj.respond_to?(cl.to_sym).should == false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'is resilient to flattening' do
|
102
|
+
## inside of arrays:
|
103
|
+
obj = @myclass.new([0,1,2])
|
104
|
+
y = @myclass.new([0,nil,1])
|
105
|
+
result = [[obj,[y]],nil].flatten
|
106
|
+
|
107
|
+
# [[0,1,2], [0,nil,1], nil]
|
108
|
+
# result.should == [[0,1,2], [0,nil,1], nil]
|
109
|
+
result[0][0].should == 0
|
110
|
+
result[0][1].should == 1
|
111
|
+
result[0][2].should == 2
|
112
|
+
|
113
|
+
result[1][0].should == 0
|
114
|
+
result[1][1].should == nil
|
115
|
+
result[1][2].should == 1
|
116
|
+
|
117
|
+
result[2].should == nil
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'is hashed like an object' do
|
121
|
+
# arrays are hashed by values so that identical arrays give same key:
|
122
|
+
hash = { [1,2,3] => 'dog'}
|
123
|
+
hash[[1,2,3]].should == hash[[1,2,3]]
|
124
|
+
|
125
|
+
# these are objects and so two different objects should hash different,
|
126
|
+
# even with the same values!
|
127
|
+
ar = [1,2,3]
|
128
|
+
one = @myclass.new(ar)
|
129
|
+
same_data_diff_object = @myclass.new(ar)
|
130
|
+
hash = { one => 'dog' }
|
131
|
+
hash[one].should == hash[one]
|
132
|
+
hash[one].should_not == hash[same_data_diff_object]
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'gives inspect (like Struct)' do
|
136
|
+
obj = @myclass.new
|
137
|
+
obj.f0 = 1
|
138
|
+
obj.f1 = 'string'
|
139
|
+
obj.inspect.should =~ /<.*f0=1, f1="string", f2="nil">/
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'can be inherited and add members' do
|
143
|
+
|
144
|
+
# the size should not change of original guy after inheritance
|
145
|
+
y = @myclass.new
|
146
|
+
y_size = y.size
|
147
|
+
class Inherited < @myclass
|
148
|
+
self.add_member(:f3)
|
149
|
+
self.add_member(:f4)
|
150
|
+
end
|
151
|
+
y_size.should == @myclass.new.size
|
152
|
+
|
153
|
+
obj = Inherited.new([0,-1,-2,1,2])
|
154
|
+
obj.f0.should == 0
|
155
|
+
obj[1].should == -1
|
156
|
+
obj.f3.should == 1
|
157
|
+
obj[4].should == 2
|
158
|
+
obj.is_a?(Inherited).should be_true
|
159
|
+
obj.is_a?(@myclass).should be_true
|
160
|
+
obj.is_a?(Array).should_not be_true
|
6
161
|
|
7
|
-
|
8
|
-
before(:all) do
|
9
|
-
@generator = ArrayClass
|
162
|
+
obj.size.should_not == y_size
|
10
163
|
end
|
11
|
-
it_should_behave_like 'a light memory class'
|
12
164
|
|
13
165
|
end
|
data/specs/light_class_shared.rb
CHANGED
@@ -1,152 +1 @@
|
|
1
1
|
|
2
|
-
describe 'a light memory class', :shared => true do
|
3
|
-
before(:each) do
|
4
|
-
@attrs = %w(f0 f1 f2)
|
5
|
-
@attrs_as_symbols = @attrs.map {|v| v.to_sym }
|
6
|
-
@myclass = @generator.new(@attrs)
|
7
|
-
@init_array = [0,1,2]
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'can be initialized with an array' do
|
11
|
-
obj = @myclass.new(@init_array)
|
12
|
-
obj.f0.should == obj[0]
|
13
|
-
obj.f1.should == obj[1]
|
14
|
-
obj.f2.should == obj[2]
|
15
|
-
obj.f1 = 5
|
16
|
-
obj.f1.should == 5
|
17
|
-
obj.class.should == @myclass
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'can be queried and modified by method call' do
|
21
|
-
obj = @myclass.new(@init_array)
|
22
|
-
obj.f0.should == 0
|
23
|
-
obj.f1.should == 1
|
24
|
-
obj.f2.should == 2
|
25
|
-
|
26
|
-
obj.f0 = 5
|
27
|
-
obj.f1 = 6
|
28
|
-
obj.f2 = 7
|
29
|
-
|
30
|
-
obj.f0.should == 5
|
31
|
-
obj.f1.should == 6
|
32
|
-
obj.f2.should == 7
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'can be queried and modified by array index' do
|
36
|
-
obj = @myclass.new(@init_array)
|
37
|
-
obj[0].should == 0
|
38
|
-
obj[1].should == 1
|
39
|
-
obj[2].should == 2
|
40
|
-
|
41
|
-
obj[0] = 5
|
42
|
-
obj[1] = 6
|
43
|
-
obj[2] = 7
|
44
|
-
|
45
|
-
obj[0].should == 5
|
46
|
-
obj[1].should == 6
|
47
|
-
obj[2].should == 7
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'can have methods added' do
|
51
|
-
MyClass = @generator.new([:one])
|
52
|
-
class MyClass
|
53
|
-
def hello() 'hiya' end
|
54
|
-
end
|
55
|
-
obj = MyClass.new
|
56
|
-
obj.hello.should == 'hiya'
|
57
|
-
# OR -->
|
58
|
-
#@myclass.class_eval("def hello() 'hiya' end")
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'can be initialized with a hash' do
|
62
|
-
## Hash initialization:
|
63
|
-
obj = @myclass.new(:f0 => 0, :f1 => 1, :f2 => 2)
|
64
|
-
obj.f0.should == 0
|
65
|
-
obj.f1.should == 1
|
66
|
-
obj.f2.should == 2
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'can set and retrieve multiple members in a single slice' do
|
70
|
-
obj = @myclass.new
|
71
|
-
obj[0,3] = [4,5,6]
|
72
|
-
obj[0].should == 4
|
73
|
-
obj[1].should == 5
|
74
|
-
obj[2].should == 6
|
75
|
-
obj[0..-1] = [7,8,9]
|
76
|
-
obj[0].should == 7
|
77
|
-
obj[1].should == 8
|
78
|
-
obj[2].should == 9
|
79
|
-
|
80
|
-
obj[0,2].should == [7,8]
|
81
|
-
obj[0..1].should == [7,8]
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'has a member list (like Struct)' do
|
85
|
-
obj = @myclass.new
|
86
|
-
obj.members.should == @attrs_as_symbols
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'does not respond to calls that would potentially mess up your object' do
|
90
|
-
%w(flatten flatten! assoc join rassoc push pop <<).each do |cl|
|
91
|
-
obj = @myclass.new
|
92
|
-
obj.respond_to?(cl.to_sym).should == false
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'is resilient to flattening' do
|
97
|
-
## inside of arrays:
|
98
|
-
obj = @myclass.new([0,1,2])
|
99
|
-
y = @myclass.new([0,nil,1])
|
100
|
-
result = [[obj,[y]],nil].flatten
|
101
|
-
|
102
|
-
# [[0,1,2], [0,nil,1], nil]
|
103
|
-
# result.should == [[0,1,2], [0,nil,1], nil]
|
104
|
-
result[0][0].should == 0
|
105
|
-
result[0][1].should == 1
|
106
|
-
result[0][2].should == 2
|
107
|
-
|
108
|
-
result[1][0].should == 0
|
109
|
-
result[1][1].should == nil
|
110
|
-
result[1][2].should == 1
|
111
|
-
|
112
|
-
result[2].should == nil
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'is hashed like an object' do
|
116
|
-
# arrays are hashed by values so that identical arrays give same key:
|
117
|
-
hash = { [1,2,3] => 'dog'}
|
118
|
-
hash[[1,2,3]].should == hash[[1,2,3]]
|
119
|
-
|
120
|
-
# these are objects and so two different objects should hash different,
|
121
|
-
# even with the same values!
|
122
|
-
ar = [1,2,3]
|
123
|
-
one = @myclass.new(ar)
|
124
|
-
same_data_diff_object = @myclass.new(ar)
|
125
|
-
hash = { one => 'dog' }
|
126
|
-
hash[one].should == hash[one]
|
127
|
-
hash[one].should_not == hash[same_data_diff_object]
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'gives inspect (like Struct)' do
|
131
|
-
obj = @myclass.new
|
132
|
-
obj.f0 = 1
|
133
|
-
obj.f1 = 'string'
|
134
|
-
obj.inspect.should =~ /<.*f0=1, f1="string", f2="nil">/
|
135
|
-
end
|
136
|
-
|
137
|
-
it 'can be inherited and add members' do
|
138
|
-
class Inherited < @myclass
|
139
|
-
self.add_member(:f3)
|
140
|
-
self.add_member(:f4)
|
141
|
-
end
|
142
|
-
obj = Inherited.new([0,-1,-2,1,2])
|
143
|
-
obj.f0.should == 0
|
144
|
-
obj[1].should == -1
|
145
|
-
obj.f3.should == 1
|
146
|
-
obj[4].should == 2
|
147
|
-
obj.is_a?(Inherited).should be_true
|
148
|
-
obj.is_a?(@myclass).should be_true
|
149
|
-
obj.is_a?(Array).should_not be_true
|
150
|
-
end
|
151
|
-
|
152
|
-
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrayclass
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Prince
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-07-
|
12
|
+
date: 2008-07-16 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -27,8 +27,8 @@ files:
|
|
27
27
|
- LICENSE
|
28
28
|
- Rakefile
|
29
29
|
- specs/spec_helper.rb
|
30
|
-
- specs/arrayclass_spec.rb
|
31
30
|
- specs/light_class_shared.rb
|
31
|
+
- specs/arrayclass_spec.rb
|
32
32
|
has_rdoc: true
|
33
33
|
homepage: http://arrayclass.rubyforge.org
|
34
34
|
post_install_message:
|
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
requirements: []
|
55
55
|
|
56
56
|
rubyforge_project: arrayclass
|
57
|
-
rubygems_version: 1.
|
57
|
+
rubygems_version: 1.2.0
|
58
58
|
signing_key:
|
59
59
|
specification_version: 2
|
60
60
|
summary: low memory class based on Array
|