Class: CompactCSV::Row

Inherits:
CSV::Row
  • Object
show all
Defined in:
lib/compact_csv.rb

Instance Method Summary collapse

Constructor Details

#initialize(headers, fields, header_row = false) ⇒ Row

Returns a new instance of Row.



13
14
15
16
17
# File 'lib/compact_csv.rb', line 13

def initialize(headers, fields, header_row = false)
  headers.freeze
  @headers = headers
  @row_values = fields
end

Instance Method Details

#<<(arg) ⇒ Object

not compatible methods

Raises:



117
118
119
# File 'lib/compact_csv.rb', line 117

def <<(arg)
  raise CompatibilityError.new("CompactCSV does not allow to append fields into row. Please use CSV module.")
end

#==(other) ⇒ Object



111
112
113
114
# File 'lib/compact_csv.rb', line 111

def ==(other)
  return row == other.row if other.is_a? CompactCSV::Row
  row == other
end

#[](index) ⇒ Object Also known as: field



84
85
86
87
88
89
90
91
# File 'lib/compact_csv.rb', line 84

def [](index)
  if index.is_a?(Integer)
    @row_values[index]
  else
    i = headers.find_index(index)
    i ? @row_values[i] : nil
  end
end

#[]=(index, value) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/compact_csv.rb', line 94

def []=(index, value)
  if index.is_a?(Integer)
    @row_values[index] = value
  else
    i = headers.find_index(index)
    @row_values[i] = value if i
  end
end

#delete(header_or_index, minimum_index = 0) ⇒ Object

Raises:



125
126
127
# File 'lib/compact_csv.rb', line 125

def delete(header_or_index, minimum_index = 0)
  raise CompatibilityError.new("CompactCSV does not allow to delete fields from row. Please use CSV module.")
end

#delete_if(&block) ⇒ Object

Raises:



129
130
131
# File 'lib/compact_csv.rb', line 129

def delete_if(&block)
  raise CompatibilityError.new("CompactCSV does not allow to delete fields from row. Please use CSV module.")
end

#each(&block) ⇒ Object



35
36
37
38
# File 'lib/compact_csv.rb', line 35

def each(&block)
  row.each(&block)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/compact_csv.rb', line 60

def empty?
  @row_values.empty?
end

#fetch(header, *varargs) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/compact_csv.rb', line 64

def fetch(header, *varargs)
  raise ArgumentError, "Too many arguments" if varargs.length > 1
  pair = row.assoc(header)
  if pair
    pair.last
  else
    if block_given?
      yield header
    elsif varargs.empty?
      raise KeyError, "key not found: #{header}"
    else
      varargs.first
    end
  end
end

#fields(*headers_and_or_indices) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/compact_csv.rb', line 40

def fields(*headers_and_or_indices)
  if headers_and_or_indices.empty?
    size_diff = @headers.size - @row_values.size
    if size_diff <= 0
      @row_values
    elsif size_diff > 0
      @row_values + Array.new(size_diff, nil)
    end
  else
    headers_and_or_indices.map do |index|
      field(index)
    end
  end
end

#has_key?(header) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/compact_csv.rb', line 80

def has_key?(header)
  !!row.assoc(header)
end

#header_row?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/compact_csv.rb', line 31

def header_row?
  false
end

#headersObject



19
20
21
# File 'lib/compact_csv.rb', line 19

def headers
  @headers
end

#push(*args) ⇒ Object

Raises:



121
122
123
# File 'lib/compact_csv.rb', line 121

def push(*args)
  raise CompatibilityError.new("CompactCSV does not allow to append fields into row. Please use CSV module.")
end

#rowObject



23
24
25
26
27
28
29
# File 'lib/compact_csv.rb', line 23

def row
  if @headers.size >= @row_values.size
    @headers.zip(@row_values).to_a
  else
    @row_values.zip(@headers).map { |pair| pair.reverse! }
  end
end

#sizeObject Also known as: length



55
56
57
# File 'lib/compact_csv.rb', line 55

def size
  [ @row_values.size, @headers.size ].max
end

#to_hashObject



103
104
105
106
107
108
109
# File 'lib/compact_csv.rb', line 103

def to_hash
  hash = {}
  @row_values.zip(headers) do |value, row|
    hash[row] = value
  end
  hash
end