lib/csv/table/table.rb in smart_csv-0.0.6 vs lib/csv/table/table.rb in smart_csv-0.0.7

- old
+ new

@@ -24,20 +24,40 @@ 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 + [ + [:lt, :<], + [:le, :<=], + [:gt, :>], + [:ge, :>=], + ].each do |name, operator| + define_method name do |attribute, value| + result = prepare_new_table - self.each do |record| - result << record if record[attribute].to_i > starter + self.each do |record| + result << record if record[attribute].to_f.send(operator, value) + end + + result end + end - result + [ + [:eq, :==], + [:ne, '!='] + ].each do |name, operator| + define_method name do |attribute, value| + result = prepare_new_table + + self.each do |record| + result << record if record[attribute].send(operator, value) + end + + result + end end def create(*conditions) headers = self.headers row = headers.inject({}){|result, value| result[value] = nil; result} @@ -64,6 +84,15 @@ while not self.empty? self.delete(0) end end end + + private + + def prepare_new_table + result = CSV::Table.new([]) + result.ancestor = self.ancestor || self + result + end + end