Module ArrayFields
In: lib/arrayfields.rb

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 Array’s - they are auto-extended when Array#fields= is called

Methods

[]   []=   at   delete_at   each_key   each_pair   each_value   each_with_field   fetch   fill   has_key?   has_value?   indexes   indices   key?   keys   member?   slice   slice!   store   to_h   to_hash   value?   values   values_at  

Classes and Modules

Class ArrayFields::FieldSet

Constants

VERSION = '3.4.0'   {{{

Public Instance methods

methods redefined to work with fields as well as numeric indexes

[Source]

# File lib/arrayfields.rb, line 58
    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

[Source]

# File lib/arrayfields.rb, line 70
    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

[Source]

# File lib/arrayfields.rb, line 84
    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

[Source]

# File lib/arrayfields.rb, line 95
    def delete_at idx
#{{{
      if @fieldset and (String === idx or Symbol === idx)
        pos = @fieldset.pos idx
        return nil unless pos
        super pos
      else
        super
      end
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 153
    def each_key
#{{{
      @fieldset.each{|field| yield field}
#}}}
    end

methods which give a hash-like interface

[Source]

# File lib/arrayfields.rb, line 146
    def each_pair
#{{{
      each_with_index do |elem, i|
        yield @fieldset.fields[i], elem
      end
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 158
    def each_value(*args, &block)
#{{{
      each(*args, &block)
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 136
    def each_with_field
#{{{
      each_with_index do |elem, i|
        yield elem, @fieldset.fields[i]
      end
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 163
    def fetch key
#{{{
      self[key] or raise IndexError, 'key not found'
#}}}
    end

[Source]

# 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

[Source]

# File lib/arrayfields.rb, line 168
    def has_key? key
#{{{
      @fieldset.fields.include? key
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 175
    def has_value? value
#{{{
      if respond_to? 'include?'
        self.include? value
      else
        a = []
        each{|val| a << val}
        a.include? value
      end
#}}}
    end
indexes(*idxs)

Alias for values_at

indices(*idxs)

Alias for values_at

key?(key)

Alias for has_key?

[Source]

# File lib/arrayfields.rb, line 187
    def keys
#{{{
      fields
#}}}
    end
member?(key)

Alias for has_key?

slice(idx, *args)

Alias for #[]

[Source]

# File lib/arrayfields.rb, line 129
    def slice!(*args)
#{{{
      ret = self[*args]
      self[*args] = nil
      ret
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 192
    def store key, value
#{{{
      self[key] = value
#}}}
    end
to_h()

Alias for to_hash

[Source]

# File lib/arrayfields.rb, line 208
    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
value?(value)

Alias for has_value?

[Source]

# File lib/arrayfields.rb, line 197
    def values
#{{{
      if respond_to? 'to_ary'
        self.to_ary
      else
        a = []
        each{|val| a << val}
        a
      end
#}}}
    end

[Source]

# File lib/arrayfields.rb, line 118
    def values_at(*idxs)
#{{{
      idxs.flatten!
      if @fieldset
        idxs.map!{|i| (String === i or Symbol === i) ? @fieldset.pos(i) : i}
      end
      super(*idxs)
#}}}
    end

[Validate]