Module: Pluckr::Schema::Conditions

Included in:
Aggregate, Exists
Defined in:
lib/pluckr/schema/conditions.rb

Overview

where: on an aggregate or existence node.

A Hash is validated once, when the schema is defined, and is therefore frozen at load time - where: { created_at: 1.week.ago.. } would keep asking about the week before boot, forever.

A callable is invoked on every compilation instead, so runtime values stay runtime values:

count :recent_orders, from: Order, where: -> { { created_at: 1.week.ago.. } }

Instance Method Summary collapse

Instance Method Details

#dynamic_where?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/pluckr/schema/conditions.rb', line 16

def dynamic_where?
  where.respond_to?(:call)
end

#resolved_whereHash?

Returns the conditions to apply to this compilation.

Returns:

  • (Hash, nil)

    the conditions to apply to this compilation



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pluckr/schema/conditions.rb', line 21

def resolved_where
  return where unless dynamic_where?

  conditions = where.call

  unless conditions.is_a?(Hash)
    raise ConfigurationError,
          "`#{output}`: a `where:` lambda must return a Hash of column => value, " \
          "got #{conditions.inspect}"
  end

  conditions.each_key { |key| validate_column!(key) }
  conditions
end