Class: EnrichmentDb::DatumModel

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ DatumModel

Initializes a new Base object

Parameters:

  • attrs (Hash) (defaults to: {})


78
79
80
# File 'lib/enrichment_db.rb', line 78

def initialize(attrs={})
  @attrs = attrs.dup
end

Instance Attribute Details

#attrsObject Also known as: to_hash

Returns the value of attribute attrs.



9
10
11
# File 'lib/enrichment_db.rb', line 9

def attrs
  @attrs
end

Class Method Details

.all(table_name, schema_name = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/enrichment_db.rb', line 53

def self.all(table_name, schema_name = nil)
  schema_name = schema_name || self::DATABASE_NAME
  puts "Finding all objects from #{table_name}"
  query = "SELECT * FROM #{schema_name}.#{table_name}"
  
  result = EnrichmentDb.request(schema_name, query).collect do |record|
    record
  end

  format_result(result)
end

.by_id(table_name, id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/enrichment_db.rb', line 28

def self.by_id(table_name, id)
  schema_name = self::DATABASE_NAME
  uid_name = self::UID_NAME
  puts "Finding object from #{table_name} with #{uid_name} = '#{id}'."
  query = "SELECT * FROM #{schema_name}.#{table_name} where #{uid_name} = $1"
  values = [id]
  
  result = EnrichmentDb.request(schema_name, query, values).collect do |record|
    record
  end

  format_result(result)
end

.by_lambda(schema_name, table_name, condition) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/enrichment_db.rb', line 42

def self.by_lambda(schema_name, table_name, condition)
  puts "Finding object from #{table_name} with condition #{condition}."
  query = "SELECT * FROM #{schema_name}.#{table_name} where #{condition}"
  
  result = EnrichmentDb.request(schema_name, query).collect do |record|
    record
  end
  
  format_result(result)
end

.format_result(result) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/enrichment_db.rb', line 65

def self.format_result(result)
  if result.size > 0 
    puts "Found #{result.size} object/s"
    result
  else
    puts "Nothing found"
  end
end

.self.lazy_attr_reader(attr) ⇒ Object .self.lazy_attr_reader(attrs) ⇒ Object

Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key

Overloads:

  • .self.lazy_attr_reader(attr) ⇒ Object

    Parameters:

    • attr (Symbol)
  • .self.lazy_attr_reader(attrs) ⇒ Object

    Parameters:

    • attrs (Array<Symbol>)


18
19
20
21
22
23
24
25
26
# File 'lib/enrichment_db.rb', line 18

def self.lazy_attr_reader(*attrs)
  attrs.each do |attribute|
    class_eval do
      define_method attribute do
        @attrs[attribute.to_s] || @attrs[attribute.to_sym]
      end
    end
  end
end