Namespace

ArrayFields

The ArrayFields module implements methods which allow an Array to be indexed by String or Symbol. It is not required to manually use this module to extend Arrays - they are auto-extended on a per-object basis when Array#fields= is called

Constants

VERSION

Public Class Methods

[](*pairs) click to toggle source
     # File lib/arrayfields.rb, line 286
286:     def self.[] *pairs
287:       new(*pairs)
288:     end
new(*pairs) click to toggle source
     # File lib/arrayfields.rb, line 276
276:     def self.new *pairs
277:       pairs = pairs.map{|pair| Enumerable === pair ? pair.to_a : pair}.flatten
278:       raise ArgumentError, "pairs must be evenly sized" unless(pairs.size % 2 == 0)
279:       (( array = [] )).fields = []
280:       0.step(pairs.size - 2, 2) do |a|
281:         b = a + 1
282:         array[ pairs[a] ] = pairs[b]
283:       end
284:       array
285:     end
version() click to toggle source
   # File lib/arrayfields.rb, line 9
9:     def self.version() Arrayfields::VERSION end

Public Instance Methods

[](idx, *args) click to toggle source

methods redefined to work with fields as well as numeric indexes

    # File lib/arrayfields.rb, line 54
54:     def [] idx, *args
55:       if @fieldset and (String === idx or Symbol === idx)
56:         pos = @fieldset.pos idx
57:         return nil unless pos
58:         super(pos, *args)
59:       else
60:         super
61:       end
62:     end
[]=(idx, *args) click to toggle source
    # File lib/arrayfields.rb, line 73
73:     def []=(idx, *args) 
74:       if @fieldset and (String === idx or Symbol === idx) 
75:         pos = @fieldset.pos idx
76:         unless pos
77:           @fieldset.fields << idx
78:           @fieldset.fieldpos[idx] = pos = size
79:         end
80:         super(pos, *args)
81:       else
82:         super
83:       end
84:     end
at(idx) click to toggle source
    # File lib/arrayfields.rb, line 85
85:     def at idx
86:       if @fieldset and (String === idx or Symbol === idx)
87:         pos = @fieldset.pos idx
88:         return nil unless pos
89:         super pos
90:       else
91:         super
92:       end
93:     end
clone() click to toggle source
     # File lib/arrayfields.rb, line 255
255:     def clone
256:       clone = super
257:     ensure
258:       clone.fields = fields.clone
259:     end
deepcopy() click to toggle source
     # File lib/arrayfields.rb, line 267
267:     def deepcopy 
268:       cp = Marshal.load(Marshal.dump(self))
269:       cp.fields = Marshal.load(Marshal.dump(self.fields))
270:       cp 
271:     end
delete_at(idx) click to toggle source
     # File lib/arrayfields.rb, line 94
 94:     def delete_at idx
 95:       if @fieldset and (String === idx or Symbol === idx)
 96:         pos = @fieldset.pos idx
 97:         return nil unless pos
 98:         new_fields = fields.dup
 99:         new_fields.delete_at(pos)
100:         self.fields = new_fields
101:         super pos
102:       else
103:         super
104:       end
105:     end
dup() click to toggle source
     # File lib/arrayfields.rb, line 261
261:     def dup
262:       dup = super
263:     ensure
264:       dup.fields = fields.dup
265:     end
each_key() click to toggle source
     # File lib/arrayfields.rb, line 157
157:     def each_key
158:       @fieldset.each{|field| yield field}
159:     end
each_pair() click to toggle source

methods which give a hash-like interface

     # File lib/arrayfields.rb, line 152
152:     def each_pair
153:       each_with_index do |elem, i|
154:         yield @fieldset.fields[i], elem
155:       end
156:     end
each_value(*args, &block) click to toggle source
     # File lib/arrayfields.rb, line 160
160:     def each_value *args, &block
161:       each(*args, &block)
162:     end
each_with_field() click to toggle source
     # File lib/arrayfields.rb, line 144
144:     def each_with_field
145:       each_with_index do |elem, i|
146:         yield elem, @fieldset.fields[i]
147:       end
148:     end
fetch(key) click to toggle source
     # File lib/arrayfields.rb, line 163
163:     def fetch key
164:       self[key] or raise IndexError, 'key not found'
165:     end
fill(obj, *args) click to toggle source
     # File lib/arrayfields.rb, line 106
106:     def fill(obj, *args)
107:       idx = args.first
108:       if idx and @fieldset and (String === idx or Symbol === idx)
109:         idx = args.shift
110:         pos = @fieldset.pos idx
111:         super(obj, pos, *args)
112:       else
113:         super
114:       end
115:     end
has_key?(key) click to toggle source
     # File lib/arrayfields.rb, line 167
167:     def has_key? key
168:       @fieldset.fields.include? key
169:     end
has_value?(value) click to toggle source
     # File lib/arrayfields.rb, line 177
177:     def has_value? value
178:       if respond_to? 'include?'
179:         self.include? value
180:       else
181:         a = []
182:         each{|val| a << val}
183:         a.include? value
184:       end
185:     end
indexes(*idxs) click to toggle source
     # File lib/arrayfields.rb, line 131
131:     def indexes(*idxs)
132:       idxs.flatten!
133:       if @fieldset
134:         idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
135:       end
136:       super(*idxs)
137:     end
indices(*idxs) click to toggle source
     # File lib/arrayfields.rb, line 124
124:     def indices(*idxs)
125:       idxs.flatten!
126:       if @fieldset
127:         idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
128:       end
129:       super(*idxs)
130:     end
invert() click to toggle source
     # File lib/arrayfields.rb, line 246
246:     def invert
247:       to_hash.invert
248:     end
key?(key) click to toggle source
     # File lib/arrayfields.rb, line 173
173:     def key? key
174:       @fieldset.fields.include? key
175:     end
keys() click to toggle source
     # File lib/arrayfields.rb, line 196
196:     def keys
197:       fields
198:     end
member?(key) click to toggle source
     # File lib/arrayfields.rb, line 170
170:     def member? key
171:       @fieldset.fields.include? key
172:     end
replace(other) click to toggle source
     # File lib/arrayfields.rb, line 243
243:     def replace other
244:       Hash === other ? update(other) : super
245:     end
slice(idx, *args) click to toggle source
    # File lib/arrayfields.rb, line 63
63:     def slice idx, *args
64:       if @fieldset and (String === idx or Symbol === idx)
65:         pos = @fieldset.pos idx
66:         return nil unless pos
67:         super(pos, *args)
68:       else
69:         super
70:       end
71:     end
slice!(*args) click to toggle source
     # File lib/arrayfields.rb, line 139
139:     def slice!(*args)
140:       ret = self[*args]
141:       self[*args] = nil
142:       ret
143:     end
store(key, value) click to toggle source
     # File lib/arrayfields.rb, line 199
199:     def store key, value
200:       self[key] = value
201:     end
to_h() click to toggle source
     # File lib/arrayfields.rb, line 225
225:     def to_h
226:       if respond_to? 'to_ary'
227:         h = {}
228:         @fieldset.fields.zip(to_ary){|f,e| h[f] = e}
229:         h
230:       else
231:         a = []
232:         each{|val| a << val}
233:         h = {}
234:         @fieldset.fields.zip(a){|f,e| h[f] = e}
235:         h
236:       end
237:     end
to_hash() click to toggle source
     # File lib/arrayfields.rb, line 212
212:     def to_hash
213:       if respond_to? 'to_ary'
214:         h = {}
215:         @fieldset.fields.zip(to_ary){|f,e| h[f] = e}
216:         h
217:       else
218:         a = []
219:         each{|val| a << val}
220:         h = {}
221:         @fieldset.fields.zip(a){|f,e| h[f] = e}
222:         h
223:       end
224:     end
to_pairs() click to toggle source
     # File lib/arrayfields.rb, line 250
250:     def to_pairs
251:       fields.zip values
252:     end
update(other) click to toggle source
     # File lib/arrayfields.rb, line 239
239:     def update other
240:       other.each{|k,v| self[k] = v}
241:       to_hash
242:     end
value?(value) click to toggle source
     # File lib/arrayfields.rb, line 186
186:     def value? value
187:       if respond_to? 'include?'
188:         self.include? value
189:       else
190:         a = []
191:         each{|val| a << val}
192:         a.include? value
193:       end
194:     end
values() click to toggle source
     # File lib/arrayfields.rb, line 202
202:     def values
203:       if respond_to? 'to_ary'
204:         self.to_ary
205:       else
206:         a = []
207:         each{|val| a << val}
208:         a
209:       end
210:     end
values_at(*idxs) click to toggle source
     # File lib/arrayfields.rb, line 117
117:     def values_at(*idxs)
118:       idxs.flatten!
119:       if @fieldset
120:         idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
121:       end
122:       super(*idxs)
123:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.