Class: Zen::Query::ApiBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/zen/query/api_block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(presence_fields:, value_fields:, block:, force: false) ⇒ ApiBlock

Returns a new instance of ApiBlock.



11
12
13
14
15
16
17
18
# File 'lib/zen/query/api_block.rb', line 11

def initialize(presence_fields:, value_fields:, block:, force: false)
  @options = extract_options!(value_fields)

  @presence_fields = presence_fields
  @value_fields = value_fields
  @block = block
  @force = force
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



9
10
11
# File 'lib/zen/query/api_block.rb', line 9

def block
  @block
end

#forceObject (readonly)

Returns the value of attribute force.



9
10
11
# File 'lib/zen/query/api_block.rb', line 9

def force
  @force
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/zen/query/api_block.rb', line 9

def options
  @options
end

#presence_fieldsObject (readonly)

Returns the value of attribute presence_fields.



9
10
11
# File 'lib/zen/query/api_block.rb', line 9

def presence_fields
  @presence_fields
end

#value_fieldsObject (readonly)

Returns the value of attribute value_fields.



9
10
11
# File 'lib/zen/query/api_block.rb', line 9

def value_fields
  @value_fields
end

Instance Method Details

#fits?(query) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/zen/query/api_block.rb', line 20

def fits?(query)
  return false unless conditions_met_by?(query)
  return true if force

  (presence_fields.empty? && value_fields.empty?) ||
    values_for(query.params).all? { |value| present?(value) }
end

#indexObject



36
37
38
39
40
41
42
43
# File 'lib/zen/query/api_block.rb', line 36

def index
  case options[:index]
  when :first  then -Float::INFINITY
  when :last   then Float::INFINITY
  when Numeric then options[:index]
  else 0
  end
end

#present?(value) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/zen/query/api_block.rb', line 32

def present?(value)
  value.respond_to?(:empty?) ? !value.empty? : !!value
end

#values_for(params) ⇒ Object



28
29
30
# File 'lib/zen/query/api_block.rb', line 28

def values_for(params)
  params.values_at(*presence_fields) + valued_values_for(params)
end