Class: Purview::Tables::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Helpers, Mixins::Logger
Defined in:
lib/purview/tables/base.rb

Direct Known Subclasses

Raw

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Logger

#logger, #logger_opts, #logger_type, #with_context_logging

Methods included from Mixins::Helpers

#blank?, #coalesced, #filter_blank_values, #filter_nil_values, #present?, #timestamp, #with_timestamp, #zero?

Constructor Details

#initialize(name, opts = {}) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
# File 'lib/purview/tables/base.rb', line 6

def initialize(name, opts={})
  @name = name
  @opts = opts
  @indices = Set.new.tap do |result|
    ((opts[:indices] || []) + default_indices).each do |index|
      index.table = self if result.add?(index)
    end
  end
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



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

def database
  @database
end

#indicesObject (readonly)

Returns the value of attribute indices.



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

def indices
  @indices
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#column_namesObject



20
21
22
# File 'lib/purview/tables/base.rb', line 20

def column_names
  columns.map(&:name)
end

#columnsObject



16
17
18
# File 'lib/purview/tables/base.rb', line 16

def columns
  opts[:columns]
end

#columns_by_nameObject



24
25
26
27
28
29
30
# File 'lib/purview/tables/base.rb', line 24

def columns_by_name
  {}.tap do |result|
    columns.each do |column|
      result[column.name] = column
    end
  end
end

#columns_of_type(type) ⇒ Object



32
33
34
# File 'lib/purview/tables/base.rb', line 32

def columns_of_type(type)
  columns.select { |column| column.is_a?(type) }
end

#created_timestamp_columnObject



36
37
38
# File 'lib/purview/tables/base.rb', line 36

def created_timestamp_column
  columns_of_type(Purview::Columns::CreatedTimestamp).first
end

#created_timestamp_indexObject



40
41
42
# File 'lib/purview/tables/base.rb', line 40

def created_timestamp_index
  Purview::Indices::Simple.new(created_timestamp_column)
end

#data_columnsObject



44
45
46
47
48
49
50
# File 'lib/purview/tables/base.rb', line 44

def data_columns
  columns - [
    created_timestamp_column,
    id_column,
    updated_timestamp_column,
  ]
end

#id_columnObject



57
58
59
# File 'lib/purview/tables/base.rb', line 57

def id_column
  columns_of_type(Purview::Columns::Id).first
end

#sync(connection, window) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/purview/tables/base.rb', line 61

def sync(connection, window)
  raw_data = puller.pull(window)
  parser.validate(raw_data)
  parsed_data = parser.parse(raw_data)
  loader.load(
    connection,
    parsed_data,
    window
  )
end

#temporary_nameObject



72
73
74
# File 'lib/purview/tables/base.rb', line 72

def temporary_name
  "#{name}_#{timestamp.to_i}"
end

#updated_timestamp_columnObject



76
77
78
# File 'lib/purview/tables/base.rb', line 76

def updated_timestamp_column
  columns_of_type(Purview::Columns::UpdatedTimestamp).first
end

#updated_timestamp_indexObject



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

def updated_timestamp_index
  Purview::Indices::Simple.new(updated_timestamp_column)
end

#window_sizeObject



84
85
86
# File 'lib/purview/tables/base.rb', line 84

def window_size
  opts[:window_size] || (60 * 60)
end