class FrozenRecord::Base

Constants

FALSY_VALUES
FIND_BY_PATTERN

Attributes

abstract_class[RW]

Public Class Methods

abstract_class?() click to toggle source
# File lib/frozen_record/base.rb, line 73
def abstract_class?
  defined?(@abstract_class) && @abstract_class
end
add_index(attribute, unique: false) click to toggle source
# File lib/frozen_record/base.rb, line 102
def add_index(attribute, unique: false)
  index = unique ? UniqueIndex.new(self, attribute) : Index.new(self, attribute)
  self.index_definitions = index_definitions.merge(index.attribute => index).freeze
end
all()
Alias for: current_scope
attributes() click to toggle source
# File lib/frozen_record/base.rb, line 66
def attributes
  @attributes ||= begin
    load_records
    @attributes
  end
end
base_path=(base_path) click to toggle source
# File lib/frozen_record/base.rb, line 59
def base_path=(base_path)
 @file_path = nil
 set_base_path(base_path)
end
Also aliased as: set_base_path
current_scope() click to toggle source
# File lib/frozen_record/base.rb, line 77
def current_scope
  store[:scope] ||= Scope.new(self)
end
Also aliased as: all
current_scope=(scope) click to toggle source
# File lib/frozen_record/base.rb, line 82
def current_scope=(scope)
  store[:scope] = scope
end
default_attributes=(default_attributes) click to toggle source
# File lib/frozen_record/base.rb, line 47
def default_attributes=(default_attributes)
  set_default_attributes(Dedup.deep_intern!(default_attributes.transform_keys(&:to_s)))
end
Also aliased as: set_default_attributes
eager_load!() click to toggle source
# File lib/frozen_record/base.rb, line 129
def eager_load!
  return if auto_reloading || abstract_class?

  load_records
end
file_path() click to toggle source
# File lib/frozen_record/base.rb, line 90
def file_path
  raise ArgumentError, "You must define `#{name}.base_path`" unless base_path
  @file_path ||= begin
    file_path = File.join(base_path, backend.filename(name))
    if !File.exist?(file_path) && File.exist?("#{file_path}.erb")
      "#{file_path}.erb"
    else
      file_path
    end
  end
end
load(attrs = {})
Alias for: new
load_records(force: false) click to toggle source
# File lib/frozen_record/base.rb, line 135
def load_records(force: false)
  if force || (auto_reloading && file_changed?)
    @records = nil
    undefine_attribute_methods
  end

  @records ||= begin
    records = backend.load(file_path)
    if default_attributes
      records = records.map { |r| assign_defaults!(r.dup).freeze }.freeze
    end
    @attributes = list_attributes(records).freeze
    define_attribute_methods(@attributes.to_a)
    records = records.map { |r| load(r) }.freeze
    index_definitions.values.each { |index| index.build(records) }
    records
  end
end
memsize(object = self, seen = Set.new.compare_by_identity) click to toggle source
# File lib/frozen_record/base.rb, line 107
def memsize(object = self, seen = Set.new.compare_by_identity)
  return 0 unless seen.add?(object)

  size = ObjectSpace.memsize_of(object)
  object.instance_variables.each { |v| size += memsize(object.instance_variable_get(v), seen) }

  case object
  when Hash
    object.each { |k, v| size += memsize(k, seen) + memsize(v, seen) }
  when Array
    object.each { |i| size += memsize(i, seen) }
  end
  size
end
new(attrs = {}) click to toggle source
# File lib/frozen_record/base.rb, line 161
def new(attrs = {})
  load(assign_defaults!(attrs.transform_keys(&:to_s)))
end
Also aliased as: load
new(attrs = {}) click to toggle source
# File lib/frozen_record/base.rb, line 213
def initialize(attrs = {})
  @attributes = attrs.freeze
end
primary_key=(primary_key) click to toggle source
# File lib/frozen_record/base.rb, line 53
def primary_key=(primary_key)
  set_primary_key(-primary_key.to_s)
end
Also aliased as: set_primary_key
respond_to_missing?(name, *) click to toggle source
# File lib/frozen_record/base.rb, line 122
def respond_to_missing?(name, *)
  if name.to_s =~ FIND_BY_PATTERN
    load_records # ensure attribute methods are defined
    return true if $1.split('_and_').all? { |attr| instance_method_already_implemented?(attr) }
  end
end
scope(name, body) click to toggle source
# File lib/frozen_record/base.rb, line 154
def scope(name, body)
  singleton_class.send(:define_method, name, &body)
end
set_base_path(base_path)
Alias for: base_path=
set_default_attributes(default_attributes)
Alias for: default_attributes=
set_primary_key(primary_key)
Alias for: primary_key=

Private Class Methods

assign_defaults!(record) click to toggle source
# File lib/frozen_record/base.rb, line 177
def assign_defaults!(record)
  if default_attributes
    default_attributes.each do |key, value|
      unless record.key?(key)
        record[key] = value
      end
    end
  end

  record
end
dynamic_match(expression, values, bang) click to toggle source
# File lib/frozen_record/base.rb, line 196
def dynamic_match(expression, values, bang)
  results = where(expression.split('_and_'.freeze).zip(values))
  bang ? results.first! : results.first
end
file_changed?() click to toggle source
# File lib/frozen_record/base.rb, line 167
def file_changed?
  last_mtime = @file_mtime
  @file_mtime = File.mtime(file_path)
  last_mtime != @file_mtime
end
list_attributes(records) click to toggle source
# File lib/frozen_record/base.rb, line 201
def list_attributes(records)
  attributes = Set.new
  records.each do |record|
    record.each_key do |key|
      attributes.add(key)
    end
  end
  attributes
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/frozen_record/base.rb, line 189
def method_missing(name, *args)
  if name.to_s =~ FIND_BY_PATTERN
    return dynamic_match($1, args, $2.present?)
  end
  super
end
store() click to toggle source
# File lib/frozen_record/base.rb, line 173
def store
  @store ||= ThreadSafeStorage.new(name)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/frozen_record/base.rb, line 230
def ==(other)
  super || other.is_a?(self.class) && other.id == id
end
[](attr) click to toggle source
# File lib/frozen_record/base.rb, line 225
def [](attr)
  @attributes[attr.to_s]
end
Also aliased as: attribute
attribute(attr)
Alias for: []
attributes() click to toggle source
# File lib/frozen_record/base.rb, line 217
def attributes
  @attributes.dup
end
id() click to toggle source
# File lib/frozen_record/base.rb, line 221
def id
  self[self.class.primary_key]
end
persisted?() click to toggle source
# File lib/frozen_record/base.rb, line 234
def persisted?
  true
end
to_key() click to toggle source
# File lib/frozen_record/base.rb, line 238
def to_key
  [id]
end

Private Instance Methods

attribute?(attribute_name) click to toggle source
# File lib/frozen_record/base.rb, line 244
def attribute?(attribute_name)
  !FALSY_VALUES.include?(self[attribute_name]) && self[attribute_name].present?
end
attribute_method?(attribute_name) click to toggle source
# File lib/frozen_record/base.rb, line 248
def attribute_method?(attribute_name)
  respond_to_without_attributes?(:attributes) && self.class.attributes.include?(attribute_name)
end