Class: Peictt::BaseModel

Inherits:
Object show all
Defined in:
lib/peictt/orm/base_model.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel



7
8
9
10
11
12
13
14
# File 'lib/peictt/orm/base_model.rb', line 7

def initialize(attributes = {})
  self.class.table = self.class.to_s.downcase.pluralize
  self.class.set_methods
  attributes.each do |key, value|
    send("#{key}=", value)
  end unless attributes.empty?
  self
end

Class Attribute Details

.table(klass = self) ⇒ Object

Returns the value of attribute table.



4
5
6
# File 'lib/peictt/orm/base_model.rb', line 4

def table
  @table
end

Class Method Details

.allObject



41
42
43
44
45
46
47
48
# File 'lib/peictt/orm/base_model.rb', line 41

def self.all
  result = DatabaseMapper.get_all self
  all = []
  result.each do |row|
    all << convert_to_object(row)
  end
  all
end

.convert_to_object(result) ⇒ Object



95
96
97
98
99
100
# File 'lib/peictt/orm/base_model.rb', line 95

def self.convert_to_object(result)
  attributes = get_columns_from_table.zip(result).to_h
  item = new attributes
  parse_string_to_time item
  item
end

.create(attributes) ⇒ Object



58
59
60
61
62
# File 'lib/peictt/orm/base_model.rb', line 58

def self.create(attributes)
  model = new(attributes)
  model.save
  find_by attributes
end

.destroy_allObject



37
38
39
# File 'lib/peictt/orm/base_model.rb', line 37

def self.destroy_all
  DatabaseMapper.destroy_all(table)
end

.find_by(attributes) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/peictt/orm/base_model.rb', line 50

def self.find_by(attributes)
  self.table = to_s.downcase.pluralize
  result = DatabaseMapper.find_by(self, attributes)
  return result if result.nil?

  convert_to_object result
end

.get_columns_from_tableObject



84
85
86
87
# File 'lib/peictt/orm/base_model.rb', line 84

def self.get_columns_from_table
  Database.connect.table_info(table).
    map { |column| column["name"] }
end

.make_methods(columns) ⇒ Object



89
90
91
92
93
# File 'lib/peictt/orm/base_model.rb', line 89

def self.make_methods(columns)
  columns.each do |column|
    attr_accessor column.to_sym
  end
end

.parse_string_to_time(model) ⇒ Object



79
80
81
82
# File 'lib/peictt/orm/base_model.rb', line 79

def self.parse_string_to_time(model)
  model.created_at = model.created_at.to_time unless model.created_at.nil?
  model.updated_at = model.updated_at.to_time unless model.updated_at.nil?
end

.parse_time_to_string(model) ⇒ Object



74
75
76
77
# File 'lib/peictt/orm/base_model.rb', line 74

def self.parse_time_to_string(model)
  model.updated_at = Time.now.to_s if model.respond_to? :updated_at
  model.created_at = model.created_at.to_s if model.respond_to? :created_at
end

.set_methodsObject



70
71
72
# File 'lib/peictt/orm/base_model.rb', line 70

def self.set_methods
  make_methods get_columns_from_table
end

Instance Method Details

#destroyObject



33
34
35
# File 'lib/peictt/orm/base_model.rb', line 33

def destroy
  DatabaseMapper.destroy self
end

#saveObject



16
17
18
19
20
21
22
23
# File 'lib/peictt/orm/base_model.rb', line 16

def save
  if @id
    self.class.parse_time_to_string self
    return DatabaseMapper.new(self, :update).save
  else
    return DatabaseMapper.new(self).save
  end
end

#update(attributes) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/peictt/orm/base_model.rb', line 25

def update(attributes)
  attributes.each do |key, value|
    send("#{key}=", value)
  end
  save
  self
end