Class: PgReports::Filter
- Inherits:
-
Object
- Object
- PgReports::Filter
- Defined in:
- lib/pg_reports/filter.rb
Overview
Applies filtering logic to report data based on YAML configuration
Constant Summary collapse
- OPERATORS =
{ "eq" => ->(a, b) { a == b }, "ne" => ->(a, b) { a != b }, "lt" => ->(a, b) { a < b }, "lte" => ->(a, b) { a <= b }, "gt" => ->(a, b) { a > b }, "gte" => ->(a, b) { a >= b } }.freeze
Instance Method Summary collapse
- #apply(data, params) ⇒ Object
-
#initialize(config) ⇒ Filter
constructor
A new instance of Filter.
Constructor Details
#initialize(config) ⇒ Filter
Returns a new instance of Filter.
15 16 17 18 19 20 |
# File 'lib/pg_reports/filter.rb', line 15 def initialize(config) @field = config["field"] @operator = config["operator"] @value_config = config["value"] @cast = config["cast"] || "string" end |
Instance Method Details
#apply(data, params) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/pg_reports/filter.rb', line 22 def apply(data, params) threshold = resolve_value(params) operator_fn = OPERATORS[@operator] raise ArgumentError, "Unknown operator: #{@operator}" unless operator_fn data.select do |row| field_value = cast_value(row[@field], @cast) operator_fn.call(field_value, threshold) end end |