Class: Purview::Tables::Base

Inherits:
Object
  • Object
show all
Includes:
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

Constructor Details

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

Returns a new instance of Base.



6
7
8
9
# File 'lib/purview/tables/base.rb', line 6

def initialize(name, opts={})
  @name = name
  @opts = opts
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

#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



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

def column_names
  columns.map(&:name)
end

#columnsObject



11
12
13
# File 'lib/purview/tables/base.rb', line 11

def columns
  opts[:columns]
end

#columns_by_nameObject



19
20
21
22
23
24
25
# File 'lib/purview/tables/base.rb', line 19

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

#columns_of_type(type) ⇒ Object



27
28
29
# File 'lib/purview/tables/base.rb', line 27

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

#created_timestamp_columnObject



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

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

#data_columnsObject



35
36
37
38
39
40
41
# File 'lib/purview/tables/base.rb', line 35

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

#id_columnObject



48
49
50
# File 'lib/purview/tables/base.rb', line 48

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

#indexed_columnsObject



52
53
54
55
56
57
# File 'lib/purview/tables/base.rb', line 52

def indexed_columns
  (opts[:indexed_columns] || []).tap do |indexed_columns|
    indexed_columns << [created_timestamp_column]
    indexed_columns << [updated_timestamp_column]
  end
end

#sync(connection, window) ⇒ Object



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

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



70
71
72
# File 'lib/purview/tables/base.rb', line 70

def temporary_name
  "#{name}_#{Time.now.utc.to_i}"
end

#updated_timestamp_columnObject



74
75
76
# File 'lib/purview/tables/base.rb', line 74

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

#window_sizeObject



78
79
80
# File 'lib/purview/tables/base.rb', line 78

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