Module: DataAPI::DataModels

Defined in:
app/models/data_api/data_models.rb

Overview

A constructor and container of DataAPI::DataModel classes that needs to be created on the fly

Class Method Summary collapse

Class Method Details

.construct(name, options = {}) ⇒ Object

Construct a new DataModel class


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/data_api/data_models.rb', line 15

def self.construct(name, options = {})
  set(name, Class.new(DataAPI::DataModel))
  model = get(name)

  model.database_url = options[:database_url] || DataAPI.database_url
  model.table_name = options[:table_name] || name.underscore
  model.organization_code = options[:organization_code]
  model.updated_at = options[:updated_at]
  model.owned_by_user = options[:owned_by_user]
  model.owner_primary_key = options[:owner_primary_key]
  model.owner_foreign_key = options[:owner_foreign_key]

  model.max_paginates_per 10_000

  model.validates options[:primary_key], uniqueness: true

  model.set_owner_relation
  model.test_db_connection

  model
end

.get(name) ⇒ Object

Get a DataModel class with its name


5
6
7
# File 'app/models/data_api/data_models.rb', line 5

def self.get(name)
  const_get(name)
end

.has?(name) ⇒ Boolean

Determine if a DataModel class exists by its name

Returns:


38
39
40
# File 'app/models/data_api/data_models.rb', line 38

def self.has?(name)
  Object.const_defined? "DataAPI::DataModels::#{name}"
end

.remove(name) ⇒ Object

Remove a DataModel class by its name


43
44
45
# File 'app/models/data_api/data_models.rb', line 43

def self.remove(name)
  remove_const(name)
end

.remove_if_exists(name) ⇒ Object

Remove a DataModel class by its name if exists


48
49
50
# File 'app/models/data_api/data_models.rb', line 48

def self.remove_if_exists(name)
  remove_const(name) if has?(name)
end

.set(name, klass) ⇒ Object

Put a class into the container


10
11
12
# File 'app/models/data_api/data_models.rb', line 10

def self.set(name, klass)
  const_set(name, klass)
end