class Parascope::Query::ApiBlock
Constants
- OPTION_KEYS
Attributes
block[R]
force[R]
options[R]
presence_fields[R]
value_fields[R]
Public Class Methods
new(presence_fields:, value_fields:, block:, force: false)
click to toggle source
# 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
Public Instance Methods
fits?(query)
click to toggle source
# 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
index()
click to toggle source
# 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)
click to toggle source
# File lib/parascope/query/api_block.rb, line 27 def present?(value) value.respond_to?(:empty?) ? !value.empty? : !!value end
values_for(params)
click to toggle source
# File lib/parascope/query/api_block.rb, line 23 def values_for(params) params.values_at(*presence_fields) + valued_values_for(params) end
Private Instance Methods
condition_met?(query, key)
click to toggle source
# File lib/parascope/query/api_block.rb, line 58 def condition_met?(query, key) return true unless options.key?(key) condition = options[key] value = case condition when String, Symbol then query.send(condition) when Proc then query.instance_exec(&condition) else condition end key == :if ? value : !value end
conditions_met_by?(query)
click to toggle source
# File lib/parascope/query/api_block.rb, line 54 def conditions_met_by?(query) condition_met?(query, :if) && condition_met?(query, :unless) end
extract_options!(fields)
click to toggle source
# File lib/parascope/query/api_block.rb, line 42 def extract_options!(fields) fields.keys.each_with_object({}) do |key, options| options[key] = fields.delete(key) if OPTION_KEYS.include?(key) end end
valued_values_for(params)
click to toggle source
# File lib/parascope/query/api_block.rb, line 48 def valued_values_for(params) value_fields.map do |field, required_value| params[field] == required_value && required_value end end