Class: Parascope::Query::ApiBlock

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ApiBlock.



8
9
10
11
12
13
# File 'lib/parascope/query/api_block.rb', line 8

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

  @presence_fields, @value_fields, @block =
    presence_fields, value_fields, block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



6
7
8
# File 'lib/parascope/query/api_block.rb', line 6

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/parascope/query/api_block.rb', line 6

def options
  @options
end

#presence_fieldsObject (readonly)

Returns the value of attribute presence_fields.



6
7
8
# File 'lib/parascope/query/api_block.rb', line 6

def presence_fields
  @presence_fields
end

#value_fieldsObject (readonly)

Returns the value of attribute value_fields.



6
7
8
# File 'lib/parascope/query/api_block.rb', line 6

def value_fields
  @value_fields
end

Instance Method Details

#fits?(query) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/parascope/query/api_block.rb', line 15

def fits?(query)
  return false unless conditions_met_by?(query)

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

#indexObject



30
31
32
# File 'lib/parascope/query/api_block.rb', line 30

def index
  options[:index] || 0
end

#present?(value) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/parascope/query/api_block.rb', line 26

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

#values_for(params) ⇒ Object



22
23
24
# File 'lib/parascope/query/api_block.rb', line 22

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