Module: Zen::Query::ApiMethods

Included in:
Zen::Query
Defined in:
lib/zen/query/api_methods.rb

Instance Method Summary collapse

Instance Method Details

#alias_subject_name(name) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/zen/query/api_methods.rb', line 6

def alias_subject_name(name)
  @subject_name = name

  define_singleton_method(name) { |&block| subject(&block) }

  alias_method(name, :subject)
end

#defaults(&block) ⇒ Object



32
33
34
35
36
# File 'lib/zen/query/api_methods.rb', line 32

def defaults(&block)
  return @defaults_block unless block_given?

  @defaults_block = block
end

#fetch_defaultsObject



38
39
40
41
42
43
44
# File 'lib/zen/query/api_methods.rb', line 38

def fetch_defaults
  ancestors
    .select { |mod| mod.respond_to?(:defaults) }
    .map(&:defaults)
    .compact
    .reduce({}) { |result, block| result.merge!(block.call) }
end

#guard(message = nil, &block) ⇒ Object



94
95
96
# File 'lib/zen/query/api_methods.rb', line 94

def guard(message = nil, &block)
  guard_blocks.push([message, block])
end

#guard_blocksObject



106
107
108
# File 'lib/zen/query/api_methods.rb', line 106

def guard_blocks
  @guard_blocks ||= []
end

#query_blocksObject



102
103
104
# File 'lib/zen/query/api_methods.rb', line 102

def query_blocks
  @query_blocks ||= []
end

#query_by(*presence_fields, **value_fields, &block) ⇒ Object Also known as: query



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

def query_by(*presence_fields, **value_fields, &block)
  query_blocks.push(
    Query::ApiBlock.new(
      presence_fields: presence_fields,
      value_fields: value_fields,
      block: block
    )
  )
end

#query_by!(*presence_fields, &block) ⇒ Object Also known as: query!



80
81
82
83
84
85
86
87
88
89
# File 'lib/zen/query/api_methods.rb', line 80

def query_by!(*presence_fields, &block)
  query_blocks.push(
    Query::ApiBlock.new(
      presence_fields: presence_fields,
      value_fields: {},
      block: block,
      force: true
    )
  )
end

#raise_on_guard_violation(value) ⇒ Object



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

def raise_on_guard_violation(value)
  @raise_on_guard_violation = !!value
end

#raise_on_guard_violation?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/zen/query/api_methods.rb', line 22

def raise_on_guard_violation?
  @raise_on_guard_violation
end

#sift_blocksObject



98
99
100
# File 'lib/zen/query/api_methods.rb', line 98

def sift_blocks
  @sift_blocks ||= []
end

#sift_by(*presence_fields, **value_fields, &block) ⇒ Object Also known as: sifter



46
47
48
49
50
51
52
53
54
# File 'lib/zen/query/api_methods.rb', line 46

def sift_by(*presence_fields, **value_fields, &block)
  sift_blocks.push(
    Query::ApiBlock.new(
      presence_fields: presence_fields,
      value_fields: value_fields,
      block: block
    )
  )
end

#sift_by!(*presence_fields, &block) ⇒ Object Also known as: sifter!



69
70
71
72
73
74
75
76
77
78
# File 'lib/zen/query/api_methods.rb', line 69

def sift_by!(*presence_fields, &block)
  sift_blocks.push(
    Query::ApiBlock.new(
      presence_fields: presence_fields,
      value_fields: {},
      block: block,
      force: true
    )
  )
end

#sorted_query_blocksObject



110
111
112
113
114
115
116
117
118
# File 'lib/zen/query/api_methods.rb', line 110

def sorted_query_blocks
  query_blocks.sort do |a, b|
    if a.index == b.index
      query_blocks.index(a) <=> query_blocks.index(b)
    else
      a.index <=> b.index
    end
  end
end

#subject(&block) ⇒ Object



26
27
28
29
30
# File 'lib/zen/query/api_methods.rb', line 26

def subject(&block)
  return @subject_block unless block_given?

  @subject_block = block
end

#subject_nameObject



14
15
16
# File 'lib/zen/query/api_methods.rb', line 14

def subject_name
  @subject_name || :subject
end