Class: Tb::FieldSet

Inherits:
Object
  • Object
show all
Defined in:
lib/tb/fieldset.rb

Overview

lib/tb/fieldset.rb - Tb::FieldSet class

Copyright © 2011-2012 Tanaka Akira <[email protected]>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.
3. The name of the author may not be used to endorse or promote
   products derived from this software without specific prior
   written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*fs) ⇒ FieldSet

Returns a new instance of FieldSet.



36
37
38
39
40
# File 'lib/tb/fieldset.rb', line 36

def initialize(*fs)
  @header = []
  @field2index = {}
  fs.each {|f| add_field(f) }
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



41
42
43
# File 'lib/tb/fieldset.rb', line 41

def header
  @header
end

Class Method Details

.normalize(header) ⇒ Object



32
33
34
# File 'lib/tb/fieldset.rb', line 32

def self.normalize(header)
  Tb::FieldSet.new(*header).header
end

Instance Method Details

#field_from_index(i) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
# File 'lib/tb/fieldset.rb', line 90

def field_from_index(i)
  raise ArgumentError, "negative index: #{i}" if i < 0
  f = @header[i]
  if f.nil?
    raise ArgumentError, "index too big: #{i}"
  end
  f
end

#field_from_index_ex(i) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
# File 'lib/tb/fieldset.rb', line 82

def field_from_index_ex(i)
  raise ArgumentError, "negative index: #{i}" if i < 0
  until i < @header.length
    add_field(nil)
  end
  @header[i]
end

#index_from_field(f) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/tb/fieldset.rb', line 74

def index_from_field(f)
  i = @field2index[f]
  if i.nil?
    raise ArgumentError, "unexpected field name: #{f.inspect}"
  end
  i
end

#index_from_field_ex(f) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tb/fieldset.rb', line 61

def index_from_field_ex(f)
  i = @field2index[f]
  return i if !i.nil?
  if /\A[1-9][0-9]*\z/ !~ f
    raise ArgumentError, "unexpected field name: #{f.inspect}"
  end
  while true
    if add_field(nil) == f
      return @header.length-1
    end
  end
end

#lengthObject



99
100
101
# File 'lib/tb/fieldset.rb', line 99

def length
  @header.length
end