class FilterTable::Table
def entries
def entries f = @resource.to_s + @filters.to_s + ' one entry' @params.map do |line| new_entry(line, f) end end
def filter_lines(table, field, condition)
def filter_lines(table, field, condition) m = case condition when Float then method(:matches_float) when Integer then method(:matches_int) when Regexp then method(:matches_regex) else method(:matches) end table.find_all do |line| next unless line.key?(field) m.call(line[field], condition) end end
def get_field(field)
def get_field(field) @params.map do |line| line[field] end end
def initialize(resource, params, filters)
def initialize(resource, params, filters) @resource = resource @params = params @filters = filters end
def matches(x, y)
def matches(x, y) x === y # rubocop:disable Style/CaseEquality end
def matches_float(x, y)
def matches_float(x, y) return false if x.nil? return false if !x.is_a?(Float) && (x =~ /\A[-+]?(\d+\.?\d*|\.\d+)\z/).nil? x.to_f == y end
def matches_int(x, y)
def matches_int(x, y) return false if x.nil? return false if !x.is_a?(Integer) && (x =~ /\A[-+]?\d+\z/).nil? x.to_i == y end
def matches_regex(x, y)
def matches_regex(x, y) return x == y if x.is_a?(Regexp) !x.to_s.match(y).nil? end
def new_entry(*_)
def new_entry(*_) fail "#{self.class} must not be used on its own. It must be inherited "\ 'and the #new_entry method must be implemented. This is an internal '\ 'error and should not happen.' end
def to_s
def to_s @resource.to_s + @filters end
def where(conditions = {}, &block)
def where(conditions = {}, &block) return self if !conditions.is_a?(Hash) return self if conditions.empty? && !block_given? filters = '' table = @params conditions.each do |field, condition| filters += " #{field} == #{condition.inspect}" table = filter_lines(table, field, condition) end if block_given? table = table.find_all { |e| new_entry(e, '').instance_eval(&block) } src = Trace.new src.instance_eval(&block) filters += Trace.to_ruby(src) end self.class.new(@resource, table, @filters + filters) end