module MoneyColumn::ActiveRecordHooks

def self.included(base)

def self.included(base)
  base.extend(ClassMethods)
end

def clear_money_column_cache

def clear_money_column_cache
  @money_column_cache.clear
end

def init_internals

def init_internals
  @money_column_cache = {}
  super
end

def initialize_dup(*)

def initialize_dup(*)
  @money_column_cache = {}
  super
end

def read_money_attribute(column)

def read_money_attribute(column)
  column = column.to_s
  options = self.class.money_column_options[column]
  return @money_column_cache[column] if @money_column_cache[column]
  value = self[column]
  return if value.nil? && !options[:coerce_null]
  @money_column_cache[column] = Money.new(value, options[:currency] || send(options[:currency_column]))
end

def reload(*)

def reload(*)
  clear_money_column_cache if persisted?
  super
end

def write_money_attribute(column, money)

def write_money_attribute(column, money)
  column = column.to_s
  options = self.class.money_column_options[column]
  @money_column_cache[column] = nil
  if money.blank?
    return self[column] = nil
  end
  unless money.is_a?(Money)
    return self[column] = Money::Helpers.value_to_decimal(money)
  end
  if options[:currency_read_only]
    currency = options[:currency] || try(options[:currency_column])
    if currency && !money.currency.compatible?(Money::Helpers.value_to_currency(currency))
      msg = "[money_column] currency mismatch between #{currency} and #{money.currency} in column #{column}."
      if Money.config.legacy_deprecations
        Money.deprecate(msg)
      else
        raise MoneyColumn::CurrencyReadOnlyError, msg
      end
    end
  else
    self[options[:currency_column]] = money.currency.to_s unless money.no_currency?
  end
  self[column] = money.value
end