module RCsvLoader::Where

Public Instance Methods

where(conditions = {}) click to toggle source

params = {

:#{accessor_name} => expected_value,
...

}

compare with '=='

options = {

:regexp => true

}

return empty array if the condition is invalid or couldn't find any rows.

# File lib/rcsv_loader/where.rb, line 18
def where conditions = {}, options = {}
  rows = @rows || []
  return rows if conditions.empty?

  # filter the conditions.
  c = conditions.select do |k, v|
        self.class.headers.keys.any? { |key| key.to_s == k.to_s }
      end

  return [] if c.empty?

  if options[:regexp]
    where_with_regexp c
  else
    rows.select { |row| c.all? { |attr, con| (row.send attr) == con } }
  end

end

Private Instance Methods

where_with_regexp(conditions = {}) click to toggle source

evaluate expression with match

# File lib/rcsv_loader/where.rb, line 42
def where_with_regexp conditions = {}
  rows = @rows || []
  return rows if conditions.empty?
  rows.select { |row| conditions.all? { |attr, con| (row.send attr).match con } }
end