Sha256: e69f71bb9b6ea984e0d68bd0bac7e803bfc0967521e96886420485ea11562125
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
#coding: utf-8 require 'csv' class CSV::Table attr_accessor :ancestor 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 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 # select all records which have attribute greater than starter def gt(attribute, starter) result = CSV::Table.new([]) result.ancestor = self.ancestor || self self.each do |record| result << record if record[attribute].to_i > starter end result end 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 record of ancestor table # it works with method #where 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 end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
smart_csv-0.0.6 | lib/csv/table/table.rb |