Class: Datasource::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/datasource/base.rb,
lib/datasource/attributes/loaded.rb,
lib/datasource/attributes/query_attribute.rb,
lib/datasource/attributes/computed_attribute.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, adapter = nil) ⇒ Base

Returns a new instance of Base.



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datasource/base.rb', line 82

def initialize(scope, adapter = nil)
  @adapter = adapter || self.class.default_adapter
  @scope =
    if self.class._update_scope
      self.class._update_scope.call(scope)
    else
      scope
    end
  @expose_attributes = []
  @expose_associations = {}
  @select_all_columns = false
  @params = {}
end

Class Attribute Details

._associationsObject

Returns the value of attribute _associations.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _associations
  @_associations
end

._attributesObject

Returns the value of attribute _attributes.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _attributes
  @_attributes
end

._collection_contextObject

Returns the value of attribute _collection_context.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _collection_context
  @_collection_context
end

._loader_orderObject

Returns the value of attribute _loader_order.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _loader_order
  @_loader_order
end

._loadersObject

Returns the value of attribute _loaders.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _loaders
  @_loaders
end

._update_scopeObject

Returns the value of attribute _update_scope.



4
5
6
# File 'lib/datasource/base.rb', line 4

def _update_scope
  @_update_scope
end

.orm_klassObject



25
26
27
# File 'lib/datasource/base.rb', line 25

def orm_klass
  fail Datasource::Error, "Model class not set for #{name}. You should define it:\nclass YourDatasource\n  @orm_klass = MyModelClass\nend"
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



80
81
82
# File 'lib/datasource/base.rb', line 80

def adapter
  @adapter
end

#expose_associationsObject (readonly)

Returns the value of attribute expose_associations.



80
81
82
# File 'lib/datasource/base.rb', line 80

def expose_associations
  @expose_associations
end

#expose_attributesObject (readonly)

Returns the value of attribute expose_attributes.



80
81
82
# File 'lib/datasource/base.rb', line 80

def expose_attributes
  @expose_attributes
end

#scopeObject (readonly)

Returns the value of attribute scope.



80
81
82
# File 'lib/datasource/base.rb', line 80

def scope
  @scope
end

Class Method Details

.collection(&block) ⇒ Object



44
45
46
# File 'lib/datasource/base.rb', line 44

def collection(&block)
  _collection_context.class_exec(&block)
end

.consumer_adapterObject



21
22
23
# File 'lib/datasource/base.rb', line 21

def consumer_adapter
  @consumer_adapter = Datasource::ConsumerAdapters::ActiveModelSerializers
end

.default_adapterObject



15
16
17
18
19
# File 'lib/datasource/base.rb', line 15

def default_adapter
  @adapter ||= begin
    Datasource::Adapters.const_get(Datasource::Adapters.constants.first)
  end
end

.inherited(base) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/datasource/base.rb', line 7

def inherited(base)
  base._attributes = (_attributes || {}).dup
  base._associations = (_associations || {}).dup
  base._loaders = (_loaders || {}).dup
  base._loader_order = (_loader_order || []).dup
  base._collection_context = Class.new(_collection_context || CollectionContext)
end

.primary_keyObject



29
30
31
# File 'lib/datasource/base.rb', line 29

def primary_key
  :id
end

.reflection_select(reflection, parent_select, assoc_select) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/datasource/base.rb', line 33

def reflection_select(reflection, parent_select, assoc_select)
  # append foreign key depending on assoication
  if reflection[:macro] == :belongs_to
    parent_select.push(reflection[:foreign_key])
  elsif [:has_many, :has_one].include?(reflection[:macro])
    assoc_select.push(reflection[:foreign_key])
  else
    fail Datasource::Error, "unsupported association type #{reflection[:macro]} - TODO"
  end
end

Instance Method Details

#attribute_exposed?(name) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/datasource/base.rb', line 229

def attribute_exposed?(name)
  @expose_attributes.include?(name.to_s)
end

#can_upgrade?(records) ⇒ Boolean

assume records have all attributes selected (default ORM record)

Returns:

  • (Boolean)


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/datasource/base.rb', line 234

def can_upgrade?(records)
  query_attributes = @expose_attributes.select do |name|
    klass = self.class._attributes[name][:klass]
    if klass
      klass.ancestors.include?(Attributes::QueryAttribute)
    end
  end

  return true if query_attributes.empty?
  Array(records).all? do |record|
    query_attributes.all? do |name|
      adapter.has_attribute?(record, name)
    end
  end
end

#fail_missing_attributes(names) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/datasource/base.rb', line 163

def fail_missing_attributes(names)
  message = if names.size > 1
    "attributes or associations #{names.join(', ')} don't exist "
  else
    "attribute or association #{names.first} doesn't exist "
  end
  message += "for #{self.class.orm_klass.name}, "
  message += "did you forget to call \"computed :#{names.first}, <dependencies>\" in your datasource_module?"
  fail Datasource::Error, message
end

#get_collection_context(rows) ⇒ Object



254
255
256
# File 'lib/datasource/base.rb', line 254

def get_collection_context(rows)
  self.class._collection_context.new(@scope, rows, self, @params)
end

#get_exposed_loadersObject



258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/datasource/base.rb', line 258

def get_exposed_loaders
  @expose_attributes
  .map { |name|
    self.class._attributes[name]
  }.select { |att|
    att[:klass] && att[:klass].ancestors.include?(Attributes::ComputedAttribute)
  }.flat_map { |att|
    att[:klass]._loader_depends
  }.uniq
  .sort_by { |loader_name|
    self.class._loader_order.index(loader_name)
  }
end

#get_select_valuesObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/datasource/base.rb', line 198

def get_select_values
  scope_table = adapter.primary_scope_table(self)
  select_values = Set.new

  if @select_all_columns
    select_values.add("#{scope_table}.*")

    self.class._attributes.values.each do |att|
      if att[:klass] && attribute_exposed?(att[:name])
        if att[:klass].ancestors.include?(Attributes::QueryAttribute)
          select_values.add("(#{att[:klass].select_value}) as #{att[:name]}")
        end
      end
    end
  else
    select_values.add("#{scope_table}.#{self.class.primary_key}")

    self.class._attributes.values.each do |att|
      if attribute_exposed?(att[:name])
        if att[:klass] == nil
          select_values.add("#{scope_table}.#{att[:name]}")
        elsif att[:klass].ancestors.include?(Attributes::QueryAttribute)
          select_values.add("(#{att[:klass].select_value}) as #{att[:name]}")
        end
      end
    end
  end

  select_values.to_a
end

#params(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/datasource/base.rb', line 96

def params(*args)
  args.each do |arg|
    if arg.kind_of?(Hash)
      @params.deep_merge!(arg.symbolize_keys)
    elsif arg.is_a?(Symbol)
      @params.merge!(arg => true)
    else
      fail Datasource::Error, "unknown parameter type #{arg.class}"
    end
  end

  @params
end

#results(rows = nil) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/datasource/base.rb', line 319

def results(rows = nil)
  rows ||= adapter.get_rows(self)

  collection_context = run_loaders(rows)

  rows.each do |row|
    row._datasource_instance = self
    set_row_loaded_values(collection_context, row) if collection_context
  end

  rows
end

#run_loaders(rows) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/datasource/base.rb', line 272

def run_loaders(rows)
  return if rows.empty?

  # check if loaders have already been ran
  if rows.first._datasource_loaded
    # not frequent, so we can afford to check all rows
    check_list = get_exposed_loaders
    run_list = []
    rows.each do |row|
      if row._datasource_loaded
        check_list.delete_if do |name|
          if !row._datasource_loaded.key?(name)
            run_list << name
            true
          end
        end
        break if check_list.empty?
      else
        run_list.concat(check_list)
        break
      end
    end
  else
    # most frequent case - loaders haven't been ran
    run_list = get_exposed_loaders
  end

  get_collection_context(rows).tap do |collection_context|
    run_list.each do |loader_name|
      loader =
        self.class._loaders[loader_name] or
        fail Datasource::Error, "loader with name :#{loader_name} could not be found"
      Datasource.logger.info { "Running loader #{loader_name} for #{rows.first.try!(:class)}" }
      collection_context.loaded_values[loader_name] = loader.load(collection_context)
    end
  end
end

#select(*names) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/datasource/base.rb', line 129

def select(*names)
  newly_exposed_attributes = []
  missing_attributes = []
  names.each do |name|
    if name.kind_of?(Hash)
      name.each_pair do |assoc_name, assoc_select|
        assoc_name = assoc_name.to_s
        if self.class._associations.key?(assoc_name)
          @expose_associations[assoc_name] ||= []
          @expose_associations[assoc_name].concat(Array(assoc_select))
          @expose_associations[assoc_name].uniq!
        else
          missing_attributes << assoc_name
        end
      end
    else
      name = name.to_s
      if name == "*"
        select_all_columns
      elsif self.class._attributes.key?(name)
        unless @expose_attributes.include?(name)
          @expose_attributes.push(name)
          newly_exposed_attributes.push(name)
        end
      else
        missing_attributes << name
      end
    end
  end
  update_dependencies(newly_exposed_attributes) unless newly_exposed_attributes.empty?
  fail_missing_attributes(missing_attributes) unless Datasource.config.simple_mode || missing_attributes.empty?
  self
end

#select_allObject



121
122
123
124
125
126
127
# File 'lib/datasource/base.rb', line 121

def select_all
  attributes = self.class._attributes.keys
  select(*attributes)
  @select_all_columns = true

  attributes
end

#select_all_columnsObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/datasource/base.rb', line 110

def select_all_columns
  column_attributes = self.class._attributes.values.select do |att|
    att[:klass].nil?
  end
  columns = column_attributes.map { |att| att[:name] }
  select(*columns)
  @select_all_columns = true

  columns
end

#set_row_loaded_values(collection_context, row) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/datasource/base.rb', line 310

def set_row_loaded_values(collection_context, row)
  row._datasource_loaded ||= {}

  primary_key = row.send(self.class.primary_key)
  collection_context.loaded_values.each_pair do |name, values|
    row._datasource_loaded[name] = values[primary_key]
  end
end

#update_dependencies(names) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/datasource/base.rb', line 174

def update_dependencies(names)
  scope_table = adapter.primary_scope_table(self)

  self.class._attributes.values.each do |att|
    next unless names.include?(att[:name])
    next unless att[:klass]

    if att[:klass].ancestors.include?(Attributes::ComputedAttribute)
      att[:klass]._depends.each_pair do |key, value|
        if key.to_s == scope_table
          select(*value)
        else
          select(key => value)
        end
      end
    elsif att[:klass].ancestors.include?(Attributes::QueryAttribute)
      att[:klass]._depends.each do |name|
        next if name == scope_table
        adapter.ensure_table_join!(self, name, att)
      end
    end
  end
end

#upgrade_records(records) ⇒ Object



250
251
252
# File 'lib/datasource/base.rb', line 250

def upgrade_records(records)
  adapter.upgrade_records(self, Array(records))
end