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:, force: false) ⇒ 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:, force: false)
  @options = extract_options!(value_fields)

  @presence_fields, @value_fields, @block, @force =
    presence_fields, value_fields, block, force
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

#forceObject (readonly)

Returns the value of attribute force.



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

def force
  @force
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
21
# File 'lib/parascope/query/api_block.rb', line 15

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

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

#indexObject



31
32
33
34
35
36
37
38
# File 'lib/parascope/query/api_block.rb', line 31

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)


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

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

#values_for(params) ⇒ Object



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

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