class CSV::Table

Attributes

ancestor[RW]

Public Instance Methods

create(*conditions) click to toggle source
# File lib/csv/table/table.rb, line 68
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_all() click to toggle source

Delete all record of ancestor table it works with method where

# File lib/csv/table/table.rb, line 85
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
not(&block) click to toggle source

Select records which don't match with block

# File lib/csv/table/table.rb, line 19
def not(&block)
  result = prepare_new_table
  result.table.replace(self.table - self.instance_eval(&block).table)
  result
end
where(*conditions) click to toggle source
# File lib/csv/table/table.rb, line 25
def where(*conditions)
  result = prepare_new_table

  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) click to toggle source
# File lib/csv/table/table.rb, line 7
def where_not(*conditions)
  result = prepare_new_table

  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

Private Instance Methods

prepare_new_table() click to toggle source
# File lib/csv/table/table.rb, line 99
def prepare_new_table
  result = CSV::Table.new([])
  result.ancestor = self.ancestor || self
  result
end