Class: CompactCSV::Row
- Inherits:
-
CSV::Row
- Object
- CSV::Row
- CompactCSV::Row
- Defined in:
- lib/compact_csv.rb
Instance Method Summary collapse
-
#<<(arg) ⇒ Object
not compatible methods.
- #==(other) ⇒ Object
- #[](index) ⇒ Object (also: #field)
- #[]=(index, value) ⇒ Object
- #delete(header_or_index, minimum_index = 0) ⇒ Object
- #delete_if(&block) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #fetch(header, *varargs) ⇒ Object
- #fields(*headers_and_or_indices) ⇒ Object
- #has_key?(header) ⇒ Boolean
- #header_row? ⇒ Boolean
- #headers ⇒ Object
-
#initialize(headers, fields, header_row = false) ⇒ Row
constructor
A new instance of Row.
- #push(*args) ⇒ Object
- #row ⇒ Object
- #size ⇒ Object (also: #length)
- #to_hash ⇒ Object
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
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
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
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
60 61 62 |
# File 'lib/compact_csv.rb', line 60 def empty? @row_values.empty? end |
#fetch(header, *varargs) ⇒ Object
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
80 81 82 |
# File 'lib/compact_csv.rb', line 80 def has_key?(header) !!row.assoc(header) end |
#header_row? ⇒ Boolean
31 32 33 |
# File 'lib/compact_csv.rb', line 31 def header_row? false end |
#headers ⇒ Object
19 20 21 |
# File 'lib/compact_csv.rb', line 19 def headers @headers end |
#push(*args) ⇒ Object
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 |
#row ⇒ Object
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 |
#size ⇒ Object Also known as: length
55 56 57 |
# File 'lib/compact_csv.rb', line 55 def size [ @row_values.size, @headers.size ].max end |
#to_hash ⇒ Object
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 |