Class: EnrichmentDb::DatumModel
- Inherits:
-
Object
- Object
- EnrichmentDb::DatumModel
- Defined in:
- lib/enrichment_db.rb
Direct Known Subclasses
Ato::Datum, Census::Datum, Datum, Geo::Locator, Geo::Region, Language::Datum
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_hash)
Returns the value of attribute attrs.
Class Method Summary collapse
- .all(table_name, schema_name = nil) ⇒ Object
- .by_id(table_name, id) ⇒ Object
- .by_lambda(schema_name, table_name, condition) ⇒ Object
- .format_result(result) ⇒ Object
-
.lazy_attr_reader(*attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key.
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ DatumModel
constructor
Initializes a new Base object.
Constructor Details
#initialize(attrs = {}) ⇒ DatumModel
Initializes a new Base object
78 79 80 |
# File 'lib/enrichment_db.rb', line 78 def initialize(attrs={}) @attrs = attrs.dup end |
Instance Attribute Details
#attrs ⇒ Object 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
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 |