Class: EventDb::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/eventdb/database.rb

Direct Known Subclasses

MemoryDatabase

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Database

Returns a new instance of Database.



6
7
8
# File 'lib/eventdb/database.rb', line 6

def initialize( config={} )
  @config = config
end

Instance Method Details

#add(events) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/eventdb/database.rb', line 31

def add( events )
  ## todo/fix: add check if Array and also allow single event
  ##  change arg to event_or_events - why? why not?

  ## todo/fix:   use attributes or to_hash or somehting that works with ActiveRecord and Struct objects - why? why not?

  events.each do |ev|
    Model::Event.create!(
        title:      ev.title,
        link:       ev.link,
        place:      ev.place,
        start_date: ev.start_date,
        end_date:   ev.end_date,
        ## note: (pre)calculate duration in days
        ##   -- mjd == Modified Julian Day Number
        ##   -- note: 0 = same day (end==start), thus always add plus one (for one day event)
        days:       ev.end_date.mjd - ev.start_date.mjd + 1
      )
  end
end

#connectObject



10
11
12
# File 'lib/eventdb/database.rb', line 10

def connect
  ActiveRecord::Base.establish_connection( @config )
end

#createObject



14
15
16
17
# File 'lib/eventdb/database.rb', line 14

def create
  CreateDb.new.up
  ConfDb::Model::Prop.create!( key: 'db.schema.event.version', value: VERSION )
end

#create_allObject



19
20
21
22
23
# File 'lib/eventdb/database.rb', line 19

def create_all
  LogDb.create # add logs table
  ConfDb.create
  create
end

#read(path) ⇒ Object

path_or_url



26
27
28
29
# File 'lib/eventdb/database.rb', line 26

def read( path )   ## path_or_url
  events = EventReader.read( path )
  add( events )
end