Class: Philtre::Grinder

Inherits:
Sequel::ASTTransformer
  • Object
show all
Defined in:
lib/philtre/grinder.rb

Overview

Using the expressions in the filter, transform a dataset with placeholders into a real dataset with expressions, for example:

ds = Personage.filter( :brief.lieu, :title.lieu ).order( :age.lieu )
g = Grinder.new( Philtre.new(title: 'Grand High Poobah', :order => :age.desc  ) )
nds = g.transform( ds )
nds.sql

=> SELECT * FROM "personages" WHERE (("title" = 'Grand High Poobah'))

In a sense, this is a means to defining SQL functions with optional keyword arguments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter = Philtre::Filter.new) ⇒ Grinder

filter must respond to expr_for( key, sql_field = nil ), expr_hash and order_hash



25
26
27
# File 'lib/philtre/grinder.rb', line 25

def initialize( filter = Philtre::Filter.new )
  @filter = filter
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



29
30
31
# File 'lib/philtre/grinder.rb', line 29

def filter
  @filter
end

Instance Method Details

#placesObject

Grouped hash of place holders in the original dataset from the last transform. Only has values after transform has been called.



67
68
69
# File 'lib/philtre/grinder.rb', line 67

def places
  @places || raise("Call transform to find place holders.")
end

#transform(dataset, apply_unknown: true) ⇒ Object Also known as: []

pass in a dataset containing PlaceHolder expressions. you’ll get back a modified dataset with the filter values filled in.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/philtre/grinder.rb', line 34

def transform( dataset, apply_unknown: true )
  @unknown = []
  @places = {}
  @subsets = []

  # handy for debugging
  @original_dataset = dataset

  # the transformed dataset with placeholders that
  # exist in filter replaced. unknown might have values
  # after this.
  t_dataset = super(dataset)
  unknown_placeholders

  if unknown.any?
    if apply_unknown
      # now filter by whatever predicates are left over
      # ie those not in the incoming dataset. Leftover
      # order parameters will overwrite existing ones
      # that are not protected by an outer select.
      filter.subset( *unknown ).apply t_dataset
    else
      raise "unknown values #{unknown.inspect} for\n#{dataset.sql}"
    end
  else
    t_dataset
  end
end

#unknownObject

collection of values in the filter that were not found as placeholders in the original dataset.



73
74
75
# File 'lib/philtre/grinder.rb', line 73

def unknown
  @unknown || raise("Call transform to find placeholders not provided by the filter.")
end