class Redis::TimeSeries::Filters

Constants

Absent
AnyValue
Equal
NoValues
NotEqual
Present
TYPES

Attributes

filters[R]

Public Class Methods

new(filters = nil) click to toggle source
# File lib/redis/time_series/filters.rb, line 114
def initialize(filters = nil)
  @filters = case filters
             when String then parse_string(filters)
             when Hash then parse_hash(filters)
             else []
             end
end

Public Instance Methods

to_a() click to toggle source
# File lib/redis/time_series/filters.rb, line 130
def to_a
  filters.map(&:to_s)
end
to_h() click to toggle source
# File lib/redis/time_series/filters.rb, line 134
def to_h
  filters.reduce({}) { |h, filter| h.merge(filter.to_h) }
end
to_s() click to toggle source
# File lib/redis/time_series/filters.rb, line 138
def to_s
  to_a.join(' ')
end
valid?() click to toggle source
# File lib/redis/time_series/filters.rb, line 126
def valid?
  !!filters.find { |f| f.is_a? Equal }
end
validate!() click to toggle source
# File lib/redis/time_series/filters.rb, line 122
def validate!
  valid? || raise(FilterError, 'Filtering requires at least one equality comparison')
end

Private Instance Methods

parse_hash(filter_hash) click to toggle source
# File lib/redis/time_series/filters.rb, line 153
def parse_hash(filter_hash)
  return unless filter_hash.is_a? Hash
  filter_hash.map do |label, value|
    case value
    when TrueClass then Present.new(label)
    when FalseClass then Absent.new(label)
    when Array then AnyValue.new(label, value)
    when Hash
      raise(FilterError, "Invalid filter hash value #{value}") unless value.keys === [:not]
      (v = value.values.first).is_a?(Array) ? NoValues.new(label, v) : NotEqual.new(label, v)
    else Equal.new(label, value)
    end
  end
end
parse_string(filter_string) click to toggle source
# File lib/redis/time_series/filters.rb, line 144
def parse_string(filter_string)
  return unless filter_string.is_a? String
  filter_string.split(' ').map do |str|
    match = TYPES.find { |f| f::REGEX.match? str }
    raise(FilterError, "Unable to parse '#{str}'") unless match
    match.parse(str)
  end
end