Class: Zen::Query

Inherits:
Object
  • Object
show all
Extended by:
ApiMethods
Includes:
Attributes
Defined in:
lib/zen/query.rb,
lib/zen/query/version.rb,
lib/zen/query/api_block.rb,
lib/zen/query/attributes.rb,
lib/zen/query/api_methods.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: ApiMethods, Attributes Classes: ApiBlock

Constant Summary collapse

UndefinedSubjectError =
Class.new(StandardError)
GuardViolationError =
Class.new(ArgumentError)
VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiMethods

alias_subject_name, defaults, fetch_defaults, guard_blocks, query_blocks, query_by, query_by!, raise_on_guard_violation, raise_on_guard_violation?, sift_blocks, sift_by, sift_by!, sorted_query_blocks, subject_name

Methods included from Attributes

included

Constructor Details

#initialize(params: {}, **attrs) ⇒ Query

Returns a new instance of Query.



29
30
31
32
33
34
# File 'lib/zen/query.rb', line 29

def initialize(params: {}, **attrs)
  @params  = klass.fetch_defaults.merge(params)
  @subject = attrs.delete(self.class.subject_name)
  @base_params = @params
  super
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



17
18
19
# File 'lib/zen/query.rb', line 17

def params
  @params
end

#subjectObject



36
37
38
# File 'lib/zen/query.rb', line 36

def subject
  @subject ||= base_subject
end

#violationObject (readonly)

Returns the value of attribute violation.



17
18
19
# File 'lib/zen/query.rb', line 17

def violation
  @violation
end

Class Method Details

.inherited(subclass) ⇒ Object

rubocop:disable Metrics/AbcSize



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

def self.inherited(subclass) # rubocop:disable Metrics/AbcSize
  subclass.raise_on_guard_violation(raise_on_guard_violation?)
  subclass.query_blocks.replace(query_blocks.dup)
  subclass.sift_blocks.replace(sift_blocks.dup)
  subclass.guard_blocks.replace(guard_blocks.dup)
  subclass.subject(&subject)
  subclass.defaults(&defaults) unless defaults.nil?
  super
end

Instance Method Details

#base_subjectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zen/query.rb', line 40

def base_subject
  subject =
    klass
      .ancestors
      .select { |mod| mod.respond_to?(:subject) }
      .map(&:subject)
      .compact
      .first
      &.call

  raise UndefinedSubjectError, "failed to build subject. Have you missed subject definition?" if subject.nil?

  subject
end

#klassObject



66
67
68
# File 'lib/zen/query.rb', line 66

def klass
  sifted? ? singleton_class : self.class
end

#resolve(*args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/zen/query.rb', line 55

def resolve(*args)
  @violation = nil
  arg_params = args.pop if args.last.is_a?(Hash)
  return sifted_instance.resolve! if arg_params.nil? && args.empty?

  clone_with_params(trues(args).merge(arg_params || {})).resolve
rescue GuardViolationError => e
  @violation = e.message
  raise if self.class.raise_on_guard_violation?
end