class BazaModels::Model

Constants

CALLBACK_TYPES

Define all callback methods.

QUERY_METHODS

Attributes

__blank_attributes[R]
db[W]
table_name[W]
changes[R]
data[RW]
db[RW]
errors[R]
table_name[W]

Public Class Methods

attribute_names() click to toggle source
# File lib/baza_models/model.rb, line 93
def self.attribute_names
  init_model
  @table.columns.map(&:name).map(&:clone)
end
column_names() click to toggle source
# File lib/baza_models/model.rb, line 248
def self.column_names
  init_model
  @column_names ||= __blank_attributes.keys.map(&:to_s)
end
columns() click to toggle source
# 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_hash() click to toggle source
# 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
db() click to toggle source
# 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
init_model(args = {}) click to toggle source
# 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
new(data = {}, args = {}) click to toggle source
# 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
reflections() click to toggle source
# 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
relationships() click to toggle source
# File lib/baza_models/model.rb, line 161
def self.relationships
  @relationships ||= {}
end
table_name() click to toggle source
# File lib/baza_models/model.rb, line 157
def self.table_name
  @table_name ||= "#{StringCases.camel_to_snake(name.gsub("::", ""))}s"
end
to_adapter() click to toggle source
# File lib/baza_models/model.rb, line 109
def self.to_adapter
  BazaModels::BazaOrmAdapter.new(class: self)
end
transaction(&blk) click to toggle source
# File lib/baza_models/model.rb, line 113
def self.transaction(&blk)
  @db.transaction(&blk)
end

Protected Class Methods

init_attribute_from_column(column) click to toggle source
# File lib/baza_models/model.rb, line 292
def self.init_attribute_from_column(column)
  column_name = column.name.to_sym

  define_method(column_name) do
    read_attribute(column_name)
  end

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

  define_method("#{column_name}=") do |new_value|
    write_attribute(column_name, new_value)
  end

  define_method("#{column_name}?") do
    !@data.fetch(column_name).to_s.strip.empty?
  end

  define_method("#{column_name}_changed?") do
    if @changes.key?(column_name) && @changes.fetch(column_name) != @data.fetch(column_name)
      true
    else
      false
    end
  end

  define_method("will_save_change_to_#{column_name}?") do
    will_save_change_to_attribute?(column_name)
  end

  define_method("#{column_name}_before_last_save") do
    attribute_before_last_save(column_name)
  end
end
model_initialized?() click to toggle source
# File lib/baza_models/model.rb, line 288
def self.model_initialized?
  @model_initialized
end

Public Instance Methods

==(other) click to toggle source
# 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) click to toggle source
# File lib/baza_models/model.rb, line 253
def [](key)
  read_attribute(key)
end
[]=(key, value) click to toggle source
# File lib/baza_models/model.rb, line 257
def []=(key, value)
  write_attribute(key, value)
end
autoloads() click to toggle source
# File lib/baza_models/model.rb, line 148
def autoloads
  @autoloads ||= {}
  @autoloads
end
changed?() click to toggle source
# 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) click to toggle source

rubocop:disable Naming/PredicateName

# 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
id() click to toggle source
# File lib/baza_models/model.rb, line 184
def id
  @data.fetch(:id)
end
inspect() click to toggle source
# 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?() click to toggle source
# File lib/baza_models/model.rb, line 79
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/baza_models/model.rb, line 83
def persisted?
  !new_record?
end
read_attribute(attribute_name) click to toggle source
# 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
reload() click to toggle source
# 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
table_name() click to toggle source
# File lib/baza_models/model.rb, line 153
def table_name
  @table_name ||= self.class.table_name
end
to_key() click to toggle source
# File lib/baza_models/model.rb, line 192
def to_key
  if new_record?
    nil
  else
    [id]
  end
end
to_model() click to toggle source
# File lib/baza_models/model.rb, line 165
def to_model
  self
end
to_param() click to toggle source
# File lib/baza_models/model.rb, line 188
def to_param
  id&.to_s
end
to_s() click to toggle source
# 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) click to toggle source
# File lib/baza_models/model.rb, line 267
def write_attribute(attribute_name, value)
  @changes[attribute_name] = value
end

Protected Instance Methods

attribute_before_last_save(attribute_name) click to toggle source
# File lib/baza_models/model.rb, line 384
def attribute_before_last_save(attribute_name)
  return @before_last_save.fetch(attribute_name) if @before_last_save.key?(attribute_name)

  @data.fetch(attribute_name)
end
fire_callbacks(name) click to toggle source
# File lib/baza_models/model.rb, line 332
def fire_callbacks(name)
  return if !@@callbacks[name] || !@@callbacks[name][self.class.name]

  @@callbacks[name][self.class.name].each do |callback_data|
    if callback_data[:block]
      instance_eval(&callback_data.fetch(:block))
    elsif callback_data[:method_name]
      method_obj = method(callback_data.fetch(:method_name))

      pass_args = callback_data.fetch(:args)
      pass_args = [] if method_obj.arity == 0

      __send__(callback_data.fetch(:method_name), *pass_args)
    else
      raise "Didn't know how to perform callbacks for #{name}"
    end
  end
end
merged_data() click to toggle source
# File lib/baza_models/model.rb, line 351
def merged_data
  @data.merge(@changes)
end
method_missing(method_name, *args, &blk) click to toggle source
Calls superclass method
# 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
real_attributes(attributes) click to toggle source

Converts attributes like “user” to “user_id” and so on.

# File lib/baza_models/model.rb, line 356
def real_attributes(attributes)
  new_attributes = {}
  attributes.each do |attribute_name, attribute_value|
    belongs_to_relations = self.class.instance_variable_get(:@belongs_to_relations)


    belongs_to_relations&.each do |relation|
      if attribute_name.to_s == relation[:relation_name].to_s
        attribute_name = :"#{attribute_name}_id"
        attribute_value = attribute_value.id if attribute_value
      end
    end

    unless has_attribute?(attribute_name)
      set_method_name = "#{attribute_name}="

      if respond_to?(set_method_name)
        __send__(set_method_name, attribute_value)
        next
      end
    end

    new_attributes[attribute_name.to_sym] = attribute_value
  end

  new_attributes
end
reset_errors() click to toggle source
# File lib/baza_models/model.rb, line 328
def reset_errors
  @errors = BazaModels::Errors.new
end
will_save_change_to_attribute?(attribute_name) click to toggle source
# File lib/baza_models/model.rb, line 390
def will_save_change_to_attribute?(attribute_name)
  return true if @changes.key?(attribute_name) && @changes[attribute_name] != data[attribute_name]

  false
end