Class: CSV::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/csv/table/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ancestorObject

Returns the value of attribute ancestor.



5
6
7
# File 'lib/csv/table/table.rb', line 5

def ancestor
  @ancestor
end

Instance Method Details

#create(*conditions) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/csv/table/table.rb', line 61

def create(*conditions)
  headers = self.headers
  row = headers.inject({}){|result, value| result[value] = nil; result}
  conditions.first.each do |key, value|
    raise "In headers: '#{headers}' don't have key: '#{key}'" unless headers.include?(key)
    row[key] = value
  end
  values = headers.inject([]) {|result, key| result << row[key]; result}
  record = CSV::Row.new(headers, values)
  self << record
  record
rescue Exception => e
  puts "#{self.class}  #{e.message}"
end

#delete_allObject

Delete all record of ancestor table it works with method #where



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/csv/table/table.rb', line 78

def delete_all
  if self.ancestor
    self.table.each do |row|
      self.ancestor.table.delete(row)
    end
  else
    while not self.empty?
      self.delete(0)
    end
  end
end

#where(*conditions) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/csv/table/table.rb', line 17

def where(*conditions)
  result = CSV::Table.new([])
  result.ancestor = self.ancestor || self

  self.each do |record|
    counter = 0
    conditions.first.each {|key, value| counter += 1 if record[key] == value.to_s}
    result << record if counter == conditions.size
  end
  result
end

#where_not(*conditions) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/csv/table/table.rb', line 7

def where_not(*conditions)
  result = CSV::Table.new([])
  self.each do |record|
    counter = 0
    conditions.first.each {|key, value| counter += 1 unless record[key] == value.to_s}
    result.table << record if counter == conditions.size
  end
  result
end