Class: RStore::Storage

Inherits:
Object show all
Includes:
HelperMethods
Defined in:
lib/rstore/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HelperMethods

#p_key

Constructor Details

#initialize(data_object, database, table_name) ⇒ Storage

Returns a new instance of Storage.

Raises:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rstore/storage.rb', line 17

def initialize data_object, database, table_name
  state = data_object.state
  raise InvalidStateError, "#{state.inspect} is not a valid state on initialization for class Storage" unless state == :converted
  @state = state
  @data  = data_object.clone
  @db    = database
  @table = table_name
  @schema = @db.schema(@table)
  @primary_key = p_key @schema 
  @prepared_data = prepare_data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/rstore/storage.rb', line 13

def data
  @data
end

#dbObject

Returns the value of attribute db.



13
14
15
# File 'lib/rstore/storage.rb', line 13

def db
  @db
end

#prepared_dataObject

Returns the value of attribute prepared_data.



13
14
15
# File 'lib/rstore/storage.rb', line 13

def prepared_data
  @prepared_data
end

#primary_keyObject

Returns the value of attribute primary_key.



13
14
15
# File 'lib/rstore/storage.rb', line 13

def primary_key
  @primary_key
end

#stateObject

Returns the value of attribute state.



14
15
16
# File 'lib/rstore/storage.rb', line 14

def state
  @state
end

#tableObject

Returns the value of attribute table.



13
14
15
# File 'lib/rstore/storage.rb', line 13

def table
  @table
end

Instance Method Details

#column_namesObject



30
31
32
33
34
# File 'lib/rstore/storage.rb', line 30

def column_names
  @schema.map do |(col_name, col_properties)|  
    col_name unless col_name == @primary_key
  end.compact
end

#insertObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rstore/storage.rb', line 45

def insert
  dataset = @db[@table]
  begin
    @db.transaction do
      @prepared_data.each_with_index do |row, row_index|
        @row_index = row_index
        dataset.insert(row)
        # Sequel often only throws an exception when retrieving an incorrect record, 
        # The following therefore is to catch invalid data of data types that are
        # not checked by RStore::Converter 
        dataset.order(@primary_key).last
      end
    end
  rescue Exception => e
    logger = Logger.new(@data)
    logger.log(:store, e, row: @row_index)
    logger.error
  
  end
  @state = :stored
  @state
end

#prepare_dataObject



37
38
39
40
41
42
# File 'lib/rstore/storage.rb', line 37

def prepare_data
  col_names = column_names
  @data.content.map do |row|
    Hash[col_names.zip(row)]
  end
end