module Arrayfields

Constants

VERSION

Public Class Methods

[](*pairs) click to toggle source
# File lib/arrayfields.rb, line 286
def self.[] *pairs
  new(*pairs)
end
new(*pairs) click to toggle source
# File lib/arrayfields.rb, line 276
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
version() click to toggle source
# File lib/arrayfields.rb, line 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

Calls superclass method
# File lib/arrayfields.rb, line 54
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
[]=(idx, *args) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 73
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
at(idx) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 85
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
clone() click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 255
def clone
  clone = super
ensure
  clone.fields = fields.clone
end
deepcopy() click to toggle source
# File lib/arrayfields.rb, line 267
def deepcopy 
  cp = Marshal.load(Marshal.dump(self))
  cp.fields = Marshal.load(Marshal.dump(self.fields))
  cp 
end
delete_at(idx) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 94
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
dup() click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 261
def dup
  dup = super
ensure
  dup.fields = fields.dup
end
each_key() { |field| ... } click to toggle source
# File lib/arrayfields.rb, line 157
def each_key
  @fieldset.each{|field| yield field}
end
each_pair() { |fields, elem| ... } click to toggle source

methods which give a hash-like interface

# File lib/arrayfields.rb, line 152
def each_pair
  each_with_index do |elem, i|
    yield @fieldset.fields[i], elem
  end
end
each_value(*args, &block) click to toggle source
# File lib/arrayfields.rb, line 160
def each_value *args, &block
  each(*args, &block)
end
each_with_field() { |elem, fields| ... } click to toggle source
# File lib/arrayfields.rb, line 144
def each_with_field
  each_with_index do |elem, i|
    yield elem, @fieldset.fields[i]
  end
end
fetch(key) click to toggle source
# File lib/arrayfields.rb, line 163
def fetch key
  self[key] or raise IndexError, 'key not found'
end
fill(obj, *args) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 106
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
has_key?(key) click to toggle source
# File lib/arrayfields.rb, line 167
def has_key? key
  @fieldset.fields.include? key
end
has_value?(value) click to toggle source
# File lib/arrayfields.rb, line 177
def has_value? value
  if respond_to? 'include?'
    self.include? value
  else
    a = []
    each{|val| a << val}
    a.include? value
  end
end
indexes(*idxs) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 131
def indexes(*idxs)
  idxs.flatten!
  if @fieldset
    idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
  end
  super(*idxs)
end
indices(*idxs) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 124
def indices(*idxs)
  idxs.flatten!
  if @fieldset
    idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
  end
  super(*idxs)
end
invert() click to toggle source
# File lib/arrayfields.rb, line 246
def invert
  to_hash.invert
end
key?(key) click to toggle source
# File lib/arrayfields.rb, line 173
def key? key
  @fieldset.fields.include? key
end
keys() click to toggle source
# File lib/arrayfields.rb, line 196
def keys
  fields
end
member?(key) click to toggle source
# File lib/arrayfields.rb, line 170
def member? key
  @fieldset.fields.include? key
end
replace(other) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 243
def replace other
  Hash === other ? update(other) : super
end
slice(idx, *args) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 63
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
slice!(*args) click to toggle source
# File lib/arrayfields.rb, line 139
def slice!(*args)
  ret = self[*args]
  self[*args] = nil
  ret
end
store(key, value) click to toggle source
# File lib/arrayfields.rb, line 199
def store key, value
  self[key] = value
end
to_h() click to toggle source
# File lib/arrayfields.rb, line 225
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
to_hash() click to toggle source
# File lib/arrayfields.rb, line 212
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
to_pairs() click to toggle source
# File lib/arrayfields.rb, line 250
def to_pairs
  fields.zip values
end
update(other) click to toggle source
# File lib/arrayfields.rb, line 239
def update other
  other.each{|k,v| self[k] = v}
  to_hash
end
value?(value) click to toggle source
# File lib/arrayfields.rb, line 186
def value? value
  if respond_to? 'include?'
    self.include? value
  else
    a = []
    each{|val| a << val}
    a.include? value
  end
end
values() click to toggle source
# File lib/arrayfields.rb, line 202
def values
  if respond_to? 'to_ary'
    self.to_ary
  else
    a = []
    each{|val| a << val}
    a
  end
end
values_at(*idxs) click to toggle source
Calls superclass method
# File lib/arrayfields.rb, line 117
def values_at(*idxs)
  idxs.flatten!
  if @fieldset
    idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
  end
  super(*idxs)
end