Class: BazaModels::Model

Inherits:
Object
  • Object
show all
Includes:
BelongsToRelations, CustomValidations, Delegation, HasManyRelations, HasOneRelations, Manipulation, Queries, Scopes, TranslationFunctionality, Validations
Defined in:
lib/baza_models/model.rb

Defined Under Namespace

Modules: BelongsToRelations, CustomValidations, Delegation, HasManyRelations, HasOneRelations, Manipulation, Queries, Scopes, TranslationFunctionality, Validations Classes: ActiveRecordColumnAdapater, Reflection

Constant Summary collapse

CALLBACK_TYPES =

Define all callback methods.

[
  :after_initialize, :after_find, :before_update, :after_update,
  :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
].freeze
QUERY_METHODS =
[
  :average, :all, :any?, :destroy_all, :each, :empty?, :ids, :maximum, :minimum, :none?, :count, :find, :first, :find_first, :last,
  :length, :size, :select, :includes, :joins, :group, :where, :order, :pluck, :preloads, :sum, :limit, :accessible_by, :ransack
].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

included, #valid?

Methods included from TranslationFunctionality

included, #model_name

Methods included from Scopes

included

Methods included from Queries

included

Methods included from Manipulation

#assign_attributes, #create, #create!, #destroy, #destroy!, included, #save, #save!, #update, #update!, #update_attributes, #update_attributes!

Methods included from HasOneRelations

included

Methods included from HasManyRelations

included

Methods included from CustomValidations

included

Methods included from Delegation

included

Methods included from BelongsToRelations

included

Constructor Details

#initialize(data = {}, args = {}) ⇒ Model



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/baza_models/model.rb', line 57

def initialize(data = {}, args = {})
  self.class.init_model

  reset_errors
  @before_last_save = {}
  @changes = {}

  if args[:init]
    @data = self.class.__blank_attributes.merge(real_attributes(data))
  else
    @data = self.class.__blank_attributes.clone
    @changes.merge!(real_attributes(data))
  end

  if @data[:id]
    @new_record = false
  else
    @new_record = true
    fire_callbacks(:after_initialize)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &blk) ⇒ Object (protected)



396
397
398
399
400
# File 'lib/baza_models/model.rb', line 396

def method_missing(method_name, *args, &blk)
  return @data.fetch(method_name) if @data.key?(method_name)

  super
end

Class Attribute Details

.__blank_attributesObject (readonly)

Returns the value of attribute __blank_attributes.



286
287
288
# File 'lib/baza_models/model.rb', line 286

def __blank_attributes
  @__blank_attributes
end

.dbObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/baza_models/model.rb', line 98

def self.db
  @db = nil if
@db&.closed?
  return @db if @db

  @db ||= BazaModels.primary_db
  raise "No Baza database has been configured" unless @db

  @db
end

.table_nameObject



157
158
159
# File 'lib/baza_models/model.rb', line 157

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

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



18
19
20
# File 'lib/baza_models/model.rb', line 18

def changes
  @changes
end

#dataObject

Returns the value of attribute data.



17
18
19
# File 'lib/baza_models/model.rb', line 17

def data
  @data
end

#dbObject

Returns the value of attribute db.



17
18
19
# File 'lib/baza_models/model.rb', line 17

def db
  @db
end

#errorsObject (readonly)

Returns the value of attribute errors.



18
19
20
# File 'lib/baza_models/model.rb', line 18

def errors
  @errors
end

#table_nameObject



153
154
155
# File 'lib/baza_models/model.rb', line 153

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

Class Method Details

.attribute_namesObject



93
94
95
96
# File 'lib/baza_models/model.rb', line 93

def self.attribute_names
  init_model
  @table.columns.map(&:name).map(&:clone)
end

.column_namesObject



248
249
250
251
# File 'lib/baza_models/model.rb', line 248

def self.column_names
  init_model
  @column_names ||= __blank_attributes.keys.map(&:to_s)
end

.columnsObject



117
118
119
120
121
122
# File 'lib/baza_models/model.rb', line 117

def self.columns
  init_model
  @table.columns.map do |column|
    BazaModels::Model::ActiveRecordColumnAdapater.new(column)
  end
end

.columns_hashObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/baza_models/model.rb', line 124

def self.columns_hash
  init_model
  result = {}

  @table.columns do |column|
    result[column.name] = BazaModels::Model::ActiveRecordColumnAdapater.new(column)
  end

  result
end

.init_model(args = {}) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/baza_models/model.rb', line 169

def self.init_model(args = {})
  return if @model_initialized && !args[:force]

  @table = db.tables[table_name]

  @__blank_attributes ||= {}

  @table.columns do |column|
    init_attribute_from_column(column) unless @model_initialized
    @__blank_attributes[column.name.to_sym] = nil
  end

  @model_initialized = true
end

.reflectionsObject



135
136
137
138
139
140
141
142
# File 'lib/baza_models/model.rb', line 135

def self.reflections
  result = {}
  relationships.each_value do |relationship|
    result[relationship.fetch(:relation_name).to_s] = BazaModels::Model::Reflection.new(relationship)
  end

  result
end

.relationshipsObject



161
162
163
# File 'lib/baza_models/model.rb', line 161

def self.relationships
  @relationships ||= {}
end

.to_adapterObject



109
110
111
# File 'lib/baza_models/model.rb', line 109

def self.to_adapter
  BazaModels::BazaOrmAdapter.new(class: self)
end

.transaction(&blk) ⇒ Object



113
114
115
# File 'lib/baza_models/model.rb', line 113

def self.transaction(&blk)
  @db.transaction(&blk)
end

Instance Method Details

#==(other) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/baza_models/model.rb', line 232

def ==(other)
  return false unless self.class == other.class

  if new_record? && other.new_record?
    merged_data == other.__send__(:merged_data)
  else
    id == other.id
  end
end

#[](key) ⇒ Object



253
254
255
# File 'lib/baza_models/model.rb', line 253

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



257
258
259
# File 'lib/baza_models/model.rb', line 257

def []=(key, value)
  write_attribute(key, value)
end

#autoloadsObject



148
149
150
151
# File 'lib/baza_models/model.rb', line 148

def autoloads
  @autoloads ||= {}
  @autoloads
end

#changed?Boolean



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/baza_models/model.rb', line 271

def changed?
  changed = false
  @changes.each do |key, value|
    next if @data.fetch(key) == value

    changed = true
    break
  end

  changed
end

#has_attribute?(name) ⇒ Boolean

rubocop:disable Naming/PredicateName



243
244
245
246
# File 'lib/baza_models/model.rb', line 243

def has_attribute?(name)
  # rubocop:enable Naming/PredicateName
  self.class.column_names.include?(name.to_s)
end

#idObject



184
185
186
# File 'lib/baza_models/model.rb', line 184

def id
  @data.fetch(:id)
end

#inspectObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/baza_models/model.rb', line 216

def inspect
  data_str = ""
  @data.each do |key, value|
    if @changes.key?(key)
      value_to_use = @changes.fetch(key)
    else
      value_to_use = value
    end

    data_str << " " unless data_str.empty?
    data_str << "#{key}=\"#{value_to_use}\""
  end

  "#<#{self.class.name} #{data_str}>"
end

#new_record?Boolean



79
80
81
# File 'lib/baza_models/model.rb', line 79

def new_record?
  @new_record
end

#persisted?Boolean



83
84
85
# File 'lib/baza_models/model.rb', line 83

def persisted?
  !new_record?
end

#read_attribute(attribute_name) ⇒ Object



261
262
263
264
265
# File 'lib/baza_models/model.rb', line 261

def read_attribute(attribute_name)
  return @changes.fetch(attribute_name) if @changes.key?(attribute_name)

  @data.fetch(attribute_name)
end

#reloadObject



200
201
202
203
204
205
206
# File 'lib/baza_models/model.rb', line 200

def reload
  @data = db.single(table_name, {id: id}, limit: 1)
  raise BazaModels::Errors::RecordNotFound unless @data

  @changes = {}
  self
end

#to_keyObject



192
193
194
195
196
197
198
# File 'lib/baza_models/model.rb', line 192

def to_key
  if new_record?
    nil
  else
    [id]
  end
end

#to_modelObject



165
166
167
# File 'lib/baza_models/model.rb', line 165

def to_model
  self
end

#to_paramObject



188
189
190
# File 'lib/baza_models/model.rb', line 188

def to_param
  id&.to_s
end

#to_sObject



208
209
210
211
212
213
214
# File 'lib/baza_models/model.rb', line 208

def to_s
  if new_record?
    "#<#{self.class.name} new!>"
  else
    "#<#{self.class.name} id=#{id}>"
  end
end

#write_attribute(attribute_name, value) ⇒ Object



267
268
269
# File 'lib/baza_models/model.rb', line 267

def write_attribute(attribute_name, value)
  @changes[attribute_name] = value
end