Class: RailsPerformance::DataSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_performance/data_source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(q: {}, type:, klass:) ⇒ DataSource

Returns a new instance of DataSource.



5
6
7
8
9
10
11
12
# File 'lib/rails_performance/data_source.rb', line 5

def initialize(q: {}, type:, klass:)
  @klass   = klass
  @type    = type
  q[:on] ||= Date.today
  @q       = q

  #puts "  [DataSource Q]  -->  #{@q.inspect}\n\n"
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/rails_performance/data_source.rb', line 3

def klass
  @klass
end

#qObject (readonly)

Returns the value of attribute q.



3
4
5
# File 'lib/rails_performance/data_source.rb', line 3

def q
  @q
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/rails_performance/data_source.rb', line 3

def type
  @type
end

Instance Method Details

#add_to(storage = RP::Models::Collection.new) ⇒ Object



26
27
28
29
30
31
# File 'lib/rails_performance/data_source.rb', line 26

def add_to(storage = RP::Models::Collection.new)
  store do |record|
    storage.add(record)
  end
  storage
end

#dbObject



14
15
16
17
18
19
20
# File 'lib/rails_performance/data_source.rb', line 14

def db
  result = RP::Models::Collection.new
  (RP::Utils.days + 1).times do |e|
    RP::DataSource.new(q: self.q.merge({ on: e.days.ago.to_date }), klass: klass, type: type).add_to(result)
  end
  result
end

#default?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rails_performance/data_source.rb', line 22

def default?
  @q.keys == [:on]
end

#storeObject



33
34
35
36
37
38
39
40
41
# File 'lib/rails_performance/data_source.rb', line 33

def store
  keys, values = Utils.fetch_from_redis(query)

  return [] if keys.blank?

  keys.each_with_index do |key, index|
    yield klass.new(key, values[index])
  end
end