structx 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.md +16 -1
- data/lib/structx.rb +40 -3
- data/lib/structx/version.rb +1 -1
- data/test/spec_structx.rb +11 -0
- metadata +9 -2
data/README.md
CHANGED
@@ -16,7 +16,7 @@ StructX is an extension of Ruby standard Struct. The diffences are that 1) the c
|
|
16
16
|
StructX.new(:x, :y, :z).new(x: 1, y: 2, z: 3) #=> #<struct x=1, y=10, z=100>
|
17
17
|
```
|
18
18
|
|
19
|
-
### Member
|
19
|
+
### Member declarations
|
20
20
|
|
21
21
|
```ruby
|
22
22
|
class A < StructX
|
@@ -38,6 +38,21 @@ end
|
|
38
38
|
B.new(1) # => #<struct B x=1, y=10, z=100>
|
39
39
|
```
|
40
40
|
|
41
|
+
### Immutable mode
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class C < StructX
|
45
|
+
immutable true
|
46
|
+
member :x
|
47
|
+
member :y
|
48
|
+
member :z
|
49
|
+
end
|
50
|
+
orig = C.new(1, 2, 3) #=> #<struct C x=1, y=2, z=3>
|
51
|
+
updated = orig.set(x: 4, y: 5, z: 6) #=> #<struct C x=4, y=5, z=6>
|
52
|
+
orig.values #=> [1, 2, 3]
|
53
|
+
updated.values #=> [4, 5, 6]
|
54
|
+
```
|
55
|
+
|
41
56
|
## Documentation
|
42
57
|
|
43
58
|
- [API Documentation](http://rubydoc.info/gems/structx)
|
data/lib/structx.rb
CHANGED
@@ -93,15 +93,24 @@ class StructX
|
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
|
+
def immutable(b=true)
|
97
|
+
@immutable = b
|
98
|
+
end
|
99
|
+
|
100
|
+
def immutable?
|
101
|
+
@immutable
|
102
|
+
end
|
103
|
+
|
96
104
|
private
|
97
105
|
|
98
106
|
def inherited(subclass)
|
99
107
|
@member.each {|key, data| subclass.member(key, data)} if @member
|
108
|
+
subclass.instance_variable_set(:@immutable, true) if @immutable
|
100
109
|
end
|
101
110
|
end
|
102
111
|
|
103
112
|
# almost methods are forwarded to value table
|
104
|
-
forward :class, :members
|
113
|
+
forward! :class, :members, :immutable?
|
105
114
|
forward :@value, :each, :each_pair
|
106
115
|
forward! :@value, :values, :length, :size, :hash
|
107
116
|
forward! lambda{|x| @value.values}, :each, :values_at
|
@@ -128,13 +137,41 @@ class StructX
|
|
128
137
|
end
|
129
138
|
end
|
130
139
|
|
140
|
+
# Same as Struct#[].
|
141
|
+
alias :get :"[]"
|
142
|
+
|
131
143
|
# Same as Struct#[]=.
|
132
144
|
def []=(idx, val)
|
133
145
|
case idx
|
134
146
|
when Integer
|
135
|
-
size > idx && -size <= idx
|
147
|
+
if size > idx && -size <= idx
|
148
|
+
if not(immutable?)
|
149
|
+
@value[members[idx]] = val
|
150
|
+
else
|
151
|
+
self.class.new(@value.merge(members[idx] => val))
|
152
|
+
end
|
153
|
+
else
|
154
|
+
raise IndexError.new(idx)
|
155
|
+
end
|
136
156
|
when Symbol, String
|
137
|
-
members.include?(idx.to_sym)
|
157
|
+
if members.include?(idx.to_sym)
|
158
|
+
if not(immutable?)
|
159
|
+
@value[idx.to_sym] = val
|
160
|
+
else
|
161
|
+
self.class.new(@value.merge(idx.to_sym => val))
|
162
|
+
end
|
163
|
+
else
|
164
|
+
raise NameError.new(idx.to_s)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# Same as #[]=, but you can set values by hash.
|
170
|
+
def set(pairs={})
|
171
|
+
if not(immutable?)
|
172
|
+
pairs.each {|idx, val| obj[idx] = val}
|
173
|
+
else
|
174
|
+
pairs.inject(self) {|obj, (idx, val)| obj.send("[]=", idx, val)}
|
138
175
|
end
|
139
176
|
end
|
140
177
|
|
data/lib/structx/version.rb
CHANGED
data/test/spec_structx.rb
CHANGED
@@ -10,6 +10,7 @@ describe "StructX" do
|
|
10
10
|
describe "StructX.new(:x, :y, :z)" do
|
11
11
|
before do
|
12
12
|
@class = StructX.new(:x, :y, :z)
|
13
|
+
@immutable = Class.new(@class).tap {|subclass| subclass.immutable}
|
13
14
|
end
|
14
15
|
|
15
16
|
it "should have 3 members" do
|
@@ -90,6 +91,10 @@ describe "StructX" do
|
|
90
91
|
x[:y].should == 2
|
91
92
|
x[:z].should == 3
|
92
93
|
should.raise(NameError) { x[:a] }
|
94
|
+
# by #get
|
95
|
+
x.get(:x).should == 1
|
96
|
+
x.get(:y).should == 2
|
97
|
+
x.get(:z).should == 3
|
93
98
|
end
|
94
99
|
end
|
95
100
|
|
@@ -111,6 +116,12 @@ describe "StructX" do
|
|
111
116
|
x[:z].should == 5
|
112
117
|
should.raise(NameError) { x[:a] = 1}
|
113
118
|
end
|
119
|
+
# immutable tests
|
120
|
+
orig = @immutable.new(1, 2, 3)
|
121
|
+
orig.values.should == [1, 2, 3]
|
122
|
+
updated = orig.set(x: 4, y: 5, z: 6)
|
123
|
+
updated.values.should == [4, 5, 6]
|
124
|
+
orig.values.should == [1, 2, 3]
|
114
125
|
end
|
115
126
|
|
116
127
|
it "should iterate values" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: forwardablex
|
@@ -144,12 +144,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
144
|
- - ! '>='
|
145
145
|
- !ruby/object:Gem::Version
|
146
146
|
version: '0'
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
hash: 1674864282059584366
|
147
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
151
|
none: false
|
149
152
|
requirements:
|
150
153
|
- - ! '>='
|
151
154
|
- !ruby/object:Gem::Version
|
152
155
|
version: '0'
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
hash: 1674864282059584366
|
153
159
|
requirements: []
|
154
160
|
rubyforge_project:
|
155
161
|
rubygems_version: 1.8.25
|
@@ -164,3 +170,4 @@ test_files:
|
|
164
170
|
- test/ruby/2.0/test_struct.rb
|
165
171
|
- test/run.sh
|
166
172
|
- test/spec_structx.rb
|
173
|
+
has_rdoc:
|