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

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
]
QUERY_METHODS =
[
  :all, :any?, :empty?, :none?, :count, :find, :first, :find_first, :last, :length, :select, :includes,
  :joins, :group, :where, :order, :limit, :to_a, :accessible_by
]

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_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
# File 'lib/baza_models/model.rb', line 56

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

  reset_errors
  @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

Class Attribute Details

.__blank_attributesObject (readonly)

Returns the value of attribute __blank_attributes.



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

def __blank_attributes
  @__blank_attributes
end

.dbObject



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

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



125
126
127
# File 'lib/baza_models/model.rb', line 125

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



121
122
123
# File 'lib/baza_models/model.rb', line 121

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

Class Method Details

.column_namesObject



213
214
215
# File 'lib/baza_models/model.rb', line 213

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

.init_modelObject



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/baza_models/model.rb', line 137

def self.init_model
  @table = db.tables[table_name]

  @__blank_attributes ||= {}

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

  @model_initialized = true
end

.ransack(params) ⇒ Object



217
218
219
# File 'lib/baza_models/model.rb', line 217

def self.ransack(params)
  BazaModels::Ransacker.new(class: self, params: params)
end

.relationshipsObject



129
130
131
# File 'lib/baza_models/model.rb', line 129

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

.to_adapterObject



102
103
104
# File 'lib/baza_models/model.rb', line 102

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

.transaction(&blk) ⇒ Object



106
107
108
# File 'lib/baza_models/model.rb', line 106

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

Instance Method Details

#==(other) ⇒ Object



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

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



221
222
223
# File 'lib/baza_models/model.rb', line 221

def [](key)
  read_attribute(key)
end

#[]=(key, value) ⇒ Object



225
226
227
# File 'lib/baza_models/model.rb', line 225

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

#autoloadsObject



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

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

#changed?Boolean

Returns:

  • (Boolean)


238
239
240
241
242
243
244
245
246
247
# File 'lib/baza_models/model.rb', line 238

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)


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

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

#idObject



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

def id
  @data.fetch(:id)
end

#inspectObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/baza_models/model.rb', line 181

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)


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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !new_record?
end

#read_attribute(attribute_name) ⇒ Object



229
230
231
232
# File 'lib/baza_models/model.rb', line 229

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

#reloadObject



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

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

#to_keyObject



158
159
160
161
162
163
164
# File 'lib/baza_models/model.rb', line 158

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

#to_modelObject



133
134
135
# File 'lib/baza_models/model.rb', line 133

def to_model
  self
end

#to_paramObject



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

def to_param
  id.to_s if id
end

#to_sObject



173
174
175
176
177
178
179
# File 'lib/baza_models/model.rb', line 173

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

#write_attribute(attribute_name, value) ⇒ Object



234
235
236
# File 'lib/baza_models/model.rb', line 234

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