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
# File lib/arrayfields.rb, line 295 def self.[] *pairs new(*pairs) end
# File lib/arrayfields.rb, line 12 def index(list) index = list.dup ensure index.fields = list end
# File lib/arrayfields.rb, line 285 def self.new *pairs pairs = pairs.map{|pair| Enumerable === pair ? pair.to_a : pair}.flatten raise ArgumentError, "pairs must be evenly sized" unless(pairs.size % 2 == 0) (( array = [] )).fields = [] 0.step(pairs.size - 2, 2) do |a| b = a + 1 array[ pairs[a] ] = pairs[b] end array end
methods redefined to work with fields as well as numeric indexes
# File lib/arrayfields.rb, line 63 def [] idx, *args if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super(pos, *args) else super end end
# File lib/arrayfields.rb, line 82 def []=(idx, *args) if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx unless pos @fieldset.fields << idx @fieldset.fieldpos[idx] = pos = size end super(pos, *args) else super end end
# File lib/arrayfields.rb, line 94 def at idx if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super pos else super end end
# File lib/arrayfields.rb, line 264 def clone clone = super ensure clone.fields = fields.clone end
# File lib/arrayfields.rb, line 276 def deepcopy cp = Marshal.load(Marshal.dump(self)) cp.fields = Marshal.load(Marshal.dump(self.fields)) cp end
# File lib/arrayfields.rb, line 103 def delete_at idx if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos new_fields = fields.dup new_fields.delete_at(pos) self.fields = new_fields super pos else super end end
# File lib/arrayfields.rb, line 270 def dup dup = super ensure dup.fields = fields.dup end
# File lib/arrayfields.rb, line 166 def each_key @fieldset.each{|field| yield field} end
methods which give a hash-like interface
# File lib/arrayfields.rb, line 161 def each_pair each_with_index do |elem, i| yield @fieldset.fields[i], elem end end
# File lib/arrayfields.rb, line 169 def each_value *args, &block each(*args, &block) end
# File lib/arrayfields.rb, line 153 def each_with_field each_with_index do |elem, i| yield elem, @fieldset.fields[i] end end
# File lib/arrayfields.rb, line 172 def fetch key self[key] or raise IndexError, 'key not found' end
# File lib/arrayfields.rb, line 115 def fill(obj, *args) idx = args.first if idx and @fieldset and (String === idx or Symbol === idx) idx = args.shift pos = @fieldset.pos idx super(obj, pos, *args) else super end end
# File lib/arrayfields.rb, line 176 def has_key? key @fieldset.fields.include? key end
# File lib/arrayfields.rb, line 186 def has_value? value if respond_to? 'include?' self.include? value else a = [] each{|val| a << val} a.include? value end end
# File lib/arrayfields.rb, line 140 def indexes(*idxs) idxs.flatten! if @fieldset idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i} end super(*idxs) end
# File lib/arrayfields.rb, line 133 def indices(*idxs) idxs.flatten! if @fieldset idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i} end super(*idxs) end
# File lib/arrayfields.rb, line 182 def key? key @fieldset.fields.include? key end
# File lib/arrayfields.rb, line 179 def member? key @fieldset.fields.include? key end
# File lib/arrayfields.rb, line 252 def replace other Hash === other ? update(other) : super end
# File lib/arrayfields.rb, line 72 def slice idx, *args if @fieldset and (String === idx or Symbol === idx) pos = @fieldset.pos idx return nil unless pos super(pos, *args) else super end end
# File lib/arrayfields.rb, line 148 def slice!(*args) ret = self[*args] self[*args] = nil ret end
# File lib/arrayfields.rb, line 208 def store key, value self[key] = value end
# File lib/arrayfields.rb, line 234 def to_h if respond_to? 'to_ary' h = {} @fieldset.fields.zip(to_ary){|f,e| h[f] = e} h else a = [] each{|val| a << val} h = {} @fieldset.fields.zip(a){|f,e| h[f] = e} h end end
# File lib/arrayfields.rb, line 221 def to_hash if respond_to? 'to_ary' h = {} @fieldset.fields.zip(to_ary){|f,e| h[f] = e} h else a = [] each{|val| a << val} h = {} @fieldset.fields.zip(a){|f,e| h[f] = e} h end end
# File lib/arrayfields.rb, line 259 def to_pairs fields.zip values end
# File lib/arrayfields.rb, line 248 def update other other.each{|k,v| self[k] = v} to_hash end
# File lib/arrayfields.rb, line 195 def value? value if respond_to? 'include?' self.include? value else a = [] each{|val| a << val} a.include? value end end
Generated with the Darkfish Rdoc Generator 2.