Class: BazaModels::Model

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

Constant Summary collapse

CALLBACK_TYPES =

Define all callback methods.

[:before_create, :after_create, :before_save, :after_save, :before_destroy, :after_destroy,
:before_validation, :after_validation, :before_validation_on_create, :after_validation_on_create,
:before_validation_on_update, :after_validation_on_update]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Model

Returns a new instance of Model.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/baza_models/model.rb', line 26

def initialize(data = {})
  self.class.init_model unless self.class.model_initialized?

  @data = data
  @changes = {}

  reset_errors

  if @data[:id]
    @new_record = false
  else
    @new_record = true
  end
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



5
6
7
# File 'lib/baza_models/model.rb', line 5

def changes
  @changes
end

#dbObject

Returns the value of attribute db.



4
5
6
# File 'lib/baza_models/model.rb', line 4

def db
  @db
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/baza_models/model.rb', line 5

def errors
  @errors
end

Class Method Details

.dbObject



50
51
52
53
# File 'lib/baza_models/model.rb', line 50

def self.db
  return @db if @db
  return BazaModels.primary_db
end

.init_attribute_from_column(column) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/baza_models/model.rb', line 72

def self.init_attribute_from_column(column)
  column_name = column.name.to_sym

  define_method(column_name) do
    return @changes.fetch(column_name) if @changes.key?(column_name)
    return @data.fetch(column_name)
  end

  define_method("#{column_name}_was") do
    return @data.fetch(column_name)
  end

  define_method("#{column_name}=") do |new_value|
    @changes[column_name] = new_value
  end
end

.init_modelObject



63
64
65
66
67
68
69
70
# File 'lib/baza_models/model.rb', line 63

def self.init_model
  @table = db.tables[table_name]
  @table.columns.each do |column_name, column|
    init_attribute_from_column(column)
  end

  @model_initialized = true
end

.model_initialized?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/baza_models/model.rb', line 89

def self.model_initialized?
  return @model_initialized
end

.table_nameObject



59
60
61
# File 'lib/baza_models/model.rb', line 59

def self.table_name
  @table_name ||= "#{StringCases.camel_to_snake(name)}s"
end

.validates(attribute_name, args) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/baza_models/model.rb', line 93

def self.validates(attribute_name, args)
  args.each do |validator_name, validator_args|
    validator_camel_name = StringCases.snake_to_camel(validator_name)
    class_name = "#{validator_camel_name}Validator"

    @@validators ||= {}
    @@validators[attribute_name] ||= []
    @@validators[attribute_name] << BazaModels::Validators.const_get(class_name).new(attribute_name, args)
  end
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



163
164
165
# File 'lib/baza_models/model.rb', line 163

def assign_attributes(attributes)
  @changes.merge!(attributes)
end

#destroyObject



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/baza_models/model.rb', line 147

def destroy
  if new_record?
    errors.add(:base, "cannot destroy new record")
    return false
  else
    fire_callbacks(:before_destroy)
    db.delete(table_name, id: id)
    fire_callbacks(:after_destroy)
    return true
  end
end

#destroy!Object



159
160
161
# File 'lib/baza_models/model.rb', line 159

def destroy!
  raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ") unless destroy
end

#idObject



104
105
106
# File 'lib/baza_models/model.rb', line 104

def id
  return @data[:id]
end

#new_record?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/baza_models/model.rb', line 41

def new_record?
  return @new_record
end

#reloadObject



141
142
143
144
145
# File 'lib/baza_models/model.rb', line 141

def reload
  @data = db.select(table_name, {id: id}, limit: 1).fetch
  @changes = {}
  return self
end

#saveObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/baza_models/model.rb', line 108

def save
  if valid?
    new_record = new_record?
    fire_callbacks(:before_save)

    if new_record
      fire_callbacks(:before_create)
      @data[:id] = db.insert(table_name, @data.merge(@changes), return_id: true)
    else
      db.update(table_name, @changes, id: id)
    end

    @changes = {}
    @new_record = false
    reload

    fire_callbacks(:after_save)
    fire_callbacks(:after_create) if new_record

    return true
  else
    return false
  end
end

#save!Object



133
134
135
136
137
138
139
# File 'lib/baza_models/model.rb', line 133

def save!
  if save
    return true
  else
    raise BazaModels::Errors::InvalidRecord
  end
end

#table_nameObject



55
56
57
# File 'lib/baza_models/model.rb', line 55

def table_name
  @table_name ||= self.class.table_name
end

#update_attributes(attributes) ⇒ Object



167
168
169
170
# File 'lib/baza_models/model.rb', line 167

def update_attributes(attributes)
  assign_attributes(attributes)
  return save
end

#update_attributes!(attributes) ⇒ Object



172
173
174
175
176
# File 'lib/baza_models/model.rb', line 172

def update_attributes!(attributes)
  unless update_attributes(attributes)
    raise BazaModels::Errors::InvalidRecord, @errors.full_messages.join(". ")
  end
end

#valid?Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/baza_models/model.rb', line 178

def valid?
  fire_callbacks(:before_validation)

  if new_record?
    fire_callbacks(:before_validation_on_create)
  else
    fire_callbacks(:before_validation_on_update)
  end

  reset_errors

  merged_data = @data.merge(@changes)
  merged_data.each do |attribute_name, attribute_value|
    next unless @@validators.key?(attribute_name)

    @@validators[attribute_name].each do |validator|
      validator.validate(self, attribute_value)
    end
  end

  fire_callbacks(:after_validation)

  if new_record?
    fire_callbacks(:after_validation_on_create)
  else
    fire_callbacks(:after_validation_on_update)
  end

  return @errors.empty?
end