Class: Pluckr::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pluckr/query.rb

Overview

Subclass this to declare a read model:

class UserSummary < Pluckr::Query
source User

schema do
  field :id
  field :email
  one :subscription do
    field :name
  end
  exists :photos
  count :videos
end
end

Class Method Summary collapse

Class Method Details

.allObject

---------------------------------------------------------- querying --

Raises:



60
61
62
63
64
# File 'lib/pluckr/query.rb', line 60

def all
  raise MissingSource, "#{name} has no `source` model to query" unless source_rooted?

  Relation.new(self, source_model.all)
end

.any?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/pluckr/query.rb', line 104

def any?(...)
  all.any?(...)
end

.compilerObject

---------------------------------------------------------- internals --



180
181
182
# File 'lib/pluckr/query.rb', line 180

def compiler
  @compiler ||= Compiler::Sql.new(schema: pluckr_schema, source_model: source_model)
end

.countObject Also known as: size

COUNT(*)/SELECT 1 over the root rows, not a fetch counted in Ruby.



95
96
97
# File 'lib/pluckr/query.rb', line 95

def count(...)
  all.count(...)
end

.declared_schemaObject

The schema declared here or by a query this one subclasses, or nil. Kept separate from pluckr_schema so the error names the class that was actually used, not an intermediate base class.



49
50
51
# File 'lib/pluckr/query.rb', line 49

def declared_schema
  @pluckr_schema || inherited_from_query(:declared_schema)
end

.empty?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/pluckr/query.rb', line 112

def empty?
  all.empty?
end

.exists?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/pluckr/query.rb', line 100

def exists?(...)
  all.exists?(...)
end

.explainObject



174
175
176
# File 'lib/pluckr/query.rb', line 174

def explain(...)
  all.explain(...)
end

.fetchObject



166
167
168
# File 'lib/pluckr/query.rb', line 166

def fetch
  source_rooted? ? all.fetch : fetch_aggregates
end

.findObject



82
83
84
# File 'lib/pluckr/query.rb', line 82

def find(...)
  all.find(...)
end

.find_byObject



86
87
88
# File 'lib/pluckr/query.rb', line 86

def find_by(...)
  all.find_by(...)
end

.find_by!Object



90
91
92
# File 'lib/pluckr/query.rb', line 90

def find_by!(...)
  all.find_by!(...)
end

.find_eachObject



116
117
118
# File 'lib/pluckr/query.rb', line 116

def find_each(...)
  all.find_each(...)
end

.firstObject



134
135
136
# File 'lib/pluckr/query.rb', line 134

def first(...)
  all.first(...)
end

.first!Object



138
139
140
# File 'lib/pluckr/query.rb', line 138

def first!
  all.first!
end

.for(records) ⇒ Object

Read models for records already in memory. One statement for a collection.

UserCard.for(user)     # one object, like find(user.id)
UserCard.for(users)    # array, same order, one SQL
UserCard.for(scope)    # an unloaded relation stays a subquery
UserCard.for(scope.limit(2)) # a page: its keys first, then one read


130
131
132
# File 'lib/pluckr/query.rb', line 130

def for(records)
  all.for(records)
end

.in_batchesObject



120
121
122
# File 'lib/pluckr/query.rb', line 120

def in_batches(...)
  all.in_batches(...)
end

.lastObject



142
143
144
# File 'lib/pluckr/query.rb', line 142

def last(...)
  all.last(...)
end

.last!Object



146
147
148
# File 'lib/pluckr/query.rb', line 146

def last!
  all.last!
end

.limit(value) ⇒ Object



74
75
76
# File 'lib/pluckr/query.rb', line 74

def limit(value)
  all.limit(value)
end

.many?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/pluckr/query.rb', line 162

def many?(...)
  all.many?(...)
end

.none?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/pluckr/query.rb', line 108

def none?(...)
  all.none?(...)
end

.offset(value) ⇒ Object



78
79
80
# File 'lib/pluckr/query.rb', line 78

def offset(value)
  all.offset(value)
end

.one?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/pluckr/query.rb', line 158

def one?(...)
  all.one?(...)
end

.orderObject



70
71
72
# File 'lib/pluckr/query.rb', line 70

def order(...)
  all.order(...)
end

.pluckr_schemaObject



42
43
44
# File 'lib/pluckr/query.rb', line 42

def pluckr_schema
  declared_schema || raise(ConfigurationError, "#{name} has no `schema` block")
end

.result_builderObject



184
185
186
# File 'lib/pluckr/query.rb', line 184

def result_builder
  @result_builder ||= Result::Builder.new(pluckr_schema, label: name || "Pluckr::Result")
end

.schema(&block) ⇒ Object



31
32
33
34
35
# File 'lib/pluckr/query.rb', line 31

def schema(&block)
  @pluckr_schema = Schema::Definition.build(source_model, &block)
  reset_compilation!
  @pluckr_schema
end

.source(model) ⇒ Object

------------------------------------------------------------- DSL --



23
24
25
26
27
28
29
# File 'lib/pluckr/query.rb', line 23

def source(model)
  unless model.is_a?(Class) && model < ActiveRecord::Base
    raise ConfigurationError, "`source` expects an ActiveRecord model, got #{model.inspect}"
  end

  @source_model = model
end

.source_modelObject

Declared on this class, or inherited from the query it subclasses.



38
39
40
# File 'lib/pluckr/query.rb', line 38

def source_model
  @source_model || inherited_from_query(:source_model)
end

.source_rooted?Boolean

Source-rooted queries return rows; aggregate-only queries return one object.

Returns:

  • (Boolean)


54
55
56
# File 'lib/pluckr/query.rb', line 54

def source_rooted?
  !source_model.nil?
end

.takeObject



150
151
152
# File 'lib/pluckr/query.rb', line 150

def take(...)
  all.take(...)
end

.take!Object



154
155
156
# File 'lib/pluckr/query.rb', line 154

def take!
  all.take!
end

.to_sqlObject



170
171
172
# File 'lib/pluckr/query.rb', line 170

def to_sql
  source_rooted? ? all.to_sql : compiler.aggregate_only_sql
end

.whereObject



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

def where(...)
  all.where(...)
end