Module: ORMakers::Model::ClassMethods
- Defined in:
- lib/ormakers/model.rb
Overview
Class methods
Instance Method Summary collapse
Instance Method Details
#all ⇒ Object
54 55 56 57 |
# File 'lib/ormakers/model.rb', line 54 def all results = Database.query("SELECT * FROM #{self.name.snake_case}s;") results.map { |result| new(result) } end |
#create(attributes = {}) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/ormakers/model.rb', line 40 def create(attributes = {}) attr_keys = attributes.keys.map(&:to_s).join(', ') attr_values = attributes.values.map { |val| "'#{val}'" }.join(', ') id = Database.query("INSERT INTO #{name.snake_case}s (#{attr_keys}) VALUES (#{attr_values}) returning *;").first['id'] attributes['id'] = id new(attributes) end |
#get(id) ⇒ Object
59 60 61 62 |
# File 'lib/ormakers/model.rb', line 59 def get(id) result = Database.query("SELECT * FROM #{self.name.snake_case}s WHERE id='#{id}';").first new(result) end |
#property(name, type) ⇒ Object
48 49 50 51 52 |
# File 'lib/ormakers/model.rb', line 48 def property(name, type) Database.query("ALTER TABLE #{self.name.snake_case}s ADD #{name} #{Database::DATA_TYPES[type]};") rescue PG::DuplicateColumn 'Property column already exists' end |