Sha256: c87204c6d88baf8292c7fe6e86fee173957a9656ded1507f8ce9500c42eb4480
Contents?: true
Size: 1.47 KB
Versions: 1
Compression:
Stored size: 1.47 KB
Contents
# frozen_string_literal: true module PlainQuery module InstanceMethods attr_reader :relation, :options def initialize(relation, options) @relation = relation @options = options @steps = self.class.steps # Validates initial relation format. Allowed only ActiveRecord::Relation. unless @relation.is_a?(ActiveRecord::Relation) raise(StandardError, 'Queries accept only ActiveRecord::Relation as input') end end def exec_query @steps.each do |step_name, params| # Handles if condition if (params[:if] && !exec_condition(params[:if])) next end # Handles unless condition if (params[:unless] && exec_condition(params[:unless])) next end # Executes query mutation and checks that step returns ActiveRecord::Relation mutated_query = send(step_name) unless mutated_query.is_a?(ActiveRecord::Relation) raise(StandardError, 'Scope must be ActiveRecord::Relation') end @relation = mutated_query end @relation end # Executes if and unless conditions, conditions must contain object method name or proc (lambda) def exec_condition(condition) if [String, Symbol].member?(condition.class) !!send(condition) elsif condition.is_a?(Proc) !!instance_exec(&condition) else raise(StandardError, 'Condition must be method name or proc') end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
plain_query-0.2.0 | lib/plain_query/instance_methods.rb |