Module: Wgit::Model

Defined in:
lib/wgit/database/model.rb

Overview

Module used to build the database collection objects, forming a data model.

Class Method Summary collapse

Class Method Details

.common_insert_dataHash

Common fields when inserting a record into the DB.

Returns:

  • (Hash)

    Insertion fields common to all models.



36
37
38
39
40
41
# File 'lib/wgit/database/model.rb', line 36

def self.common_insert_data
  {
    date_added: Wgit::Utils.time_stamp,
    date_modified: Wgit::Utils.time_stamp
  }
end

.common_update_dataHash

Common fields when updating a record in the DB.

Returns:

  • (Hash)

    Update fields common to all models.



46
47
48
49
50
# File 'lib/wgit/database/model.rb', line 46

def self.common_update_data
  {
    date_modified: Wgit::Utils.time_stamp
  }
end

.document(doc) ⇒ Hash

The data model for a Wgit::Document collection object.

Parameters:

Returns:

  • (Hash)

    The Document model ready for DB insertion.



24
25
26
27
28
29
30
31
# File 'lib/wgit/database/model.rb', line 24

def self.document(doc)
  raise 'doc must respond_to? :to_h' unless doc.respond_to?(:to_h)

  model = doc.to_h(include_html: false, include_score: false)
  model['url'] = url(doc.url) # Expand Url String into full object.

  select_bson_types(model)
end

.select_bson_types(model_hash) ⇒ Hash

Returns the model having removed non bson types (for use with MongoDB).

Parameters:

  • model_hash (Hash)

    The model Hash to sanitize.

Returns:

  • (Hash)

    The model Hash with non bson types removed.



56
57
58
# File 'lib/wgit/database/model.rb', line 56

def self.select_bson_types(model_hash)
  model_hash.select { |_k, v| v.respond_to?(:bson_type) }
end

.url(url) ⇒ Hash

The data model for a Wgit::Url collection object and for an embedded 'url' inside a Wgit::Document collection object.

Parameters:

Returns:

  • (Hash)

    The URL model ready for DB insertion.



13
14
15
16
17
18
# File 'lib/wgit/database/model.rb', line 13

def self.url(url)
  raise 'url must respond_to? :to_h' unless url.respond_to?(:to_h)

  model = url.to_h
  select_bson_types(model)
end