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_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

Returns a new instance of Model.



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

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
# 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.



284
285
286
# File 'lib/baza_models/model.rb', line 284

def __blank_attributes
  @__blank_attributes
end

.dbObject



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

def self.db
  @db = nil if @db && @db.closed?
  return @db if @db
  @db ||= BazaModels.primary_db
  raise "No Baza database has been configured" unless @db
  @db
end

.table_nameObject



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

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



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

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

Class Method Details

.attribute_namesObject



95
96
97
98
# File 'lib/baza_models/model.rb', line 95

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



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

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

.columns_hashObject



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

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



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

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



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

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



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

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

.to_adapterObject



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

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

.transaction(&blk) ⇒ Object



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

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?
    return merged_data == other.__send__(:merged_data)
  else
    return 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



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

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 Style/PredicateName

Returns:

  • (Boolean)


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

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

#idObject



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

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

rubocop:disable Style/TrivialAccessors

Returns:

  • (Boolean)


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

def new_record?
  # rubocop:enable Style/TrivialAccessors
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !new_record?
end

#read_attribute(attribute_name) ⇒ Object



261
262
263
264
# 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



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

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

#to_keyObject



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

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

#to_modelObject



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

def to_model
  self
end

#to_paramObject



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

def to_param
  id.to_s if id
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



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

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