Class: FrozenRecord::Base
- Inherits:
-
Object
- Object
- FrozenRecord::Base
show all
- Extended by:
- ActiveModel::Naming, ActiveSupport::DescendantsTracker
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml
- Defined in:
- lib/frozen_record/base.rb
Defined Under Namespace
Classes: ThreadSafeStorage
Constant Summary
collapse
- FIND_BY_PATTERN =
/\Afind_by_(\w+)(!?)/
- FALSY_VALUES =
[false, nil, 0, -''].to_set
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
216
217
218
|
# File 'lib/frozen_record/base.rb', line 216
def initialize(attrs = {})
@attributes = attrs.freeze
end
|
Class Attribute Details
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
71
72
73
|
# File 'lib/frozen_record/base.rb', line 71
def abstract_class
@abstract_class
end
|
Class Method Details
.abstract_class? ⇒ Boolean
80
81
82
|
# File 'lib/frozen_record/base.rb', line 80
def abstract_class?
defined?(@abstract_class) && @abstract_class
end
|
.add_index(attribute, unique: false) ⇒ Object
108
109
110
111
|
# File 'lib/frozen_record/base.rb', line 108
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
|
.attributes ⇒ Object
73
74
75
76
77
78
|
# File 'lib/frozen_record/base.rb', line 73
def attributes
@attributes ||= begin
load_records
@attributes
end
end
|
.base_path=(base_path) ⇒ Object
66
67
68
69
|
# File 'lib/frozen_record/base.rb', line 66
def base_path=(base_path)
@file_path = nil
set_base_path(base_path)
end
|
.current_scope ⇒ Object
Also known as:
all
84
85
86
|
# File 'lib/frozen_record/base.rb', line 84
def current_scope
store[:scope] ||= Scope.new(self)
end
|
.current_scope=(scope) ⇒ Object
89
90
91
|
# File 'lib/frozen_record/base.rb', line 89
def current_scope=(scope)
store[:scope] = scope
end
|
.default_attributes=(default_attributes) ⇒ Object
54
55
56
|
# File 'lib/frozen_record/base.rb', line 54
def default_attributes=(default_attributes)
set_default_attributes(Dedup.deep_intern!(default_attributes.stringify_keys))
end
|
.eager_load! ⇒ Object
135
136
137
138
139
|
# File 'lib/frozen_record/base.rb', line 135
def eager_load!
return if auto_reloading || abstract_class?
load_records
end
|
.file_path ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/frozen_record/base.rb', line 96
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 ⇒ Object
163
|
# File 'lib/frozen_record/base.rb', line 163
alias_method :load, :new
|
.load_records(force: false) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/frozen_record/base.rb', line 141
def load_records(force: false)
if force || (auto_reloading && file_changed?)
@records = nil
undefine_attribute_methods
end
@records ||= begin
records = backend.load(file_path)
records.each { |r| assign_defaults!(r) }
records = Dedup.deep_intern!(records)
@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) ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/frozen_record/base.rb', line 113
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 = {}) ⇒ Object
166
167
168
|
# File 'lib/frozen_record/base.rb', line 166
def new(attrs = {})
load(assign_defaults!(attrs.stringify_keys))
end
|
.primary_key=(primary_key) ⇒ Object
60
61
62
|
# File 'lib/frozen_record/base.rb', line 60
def primary_key=(primary_key)
set_primary_key(-primary_key.to_s)
end
|
.respond_to_missing?(name) ⇒ Boolean
128
129
130
131
132
133
|
# File 'lib/frozen_record/base.rb', line 128
def respond_to_missing?(name, *)
if name.to_s =~ FIND_BY_PATTERN
load_records return true if $1.split('_and_').all? { |attr| instance_method_already_implemented?(attr) }
end
end
|
.scope(name, body) ⇒ Object
159
160
161
|
# File 'lib/frozen_record/base.rb', line 159
def scope(name, body)
singleton_class.send(:define_method, name, &body)
end
|
.set_base_path ⇒ Object
64
|
# File 'lib/frozen_record/base.rb', line 64
alias_method :set_base_path, :base_path=
|
.set_default_attributes ⇒ Object
52
|
# File 'lib/frozen_record/base.rb', line 52
alias_method :set_default_attributes, :default_attributes=
|
.set_primary_key ⇒ Object
58
|
# File 'lib/frozen_record/base.rb', line 58
alias_method :set_primary_key, :primary_key=
|
Instance Method Details
#==(other) ⇒ Object
233
234
235
|
# File 'lib/frozen_record/base.rb', line 233
def ==(other)
super || other.is_a?(self.class) && other.id == id
end
|
#[](attr) ⇒ Object
Also known as:
attribute
228
229
230
|
# File 'lib/frozen_record/base.rb', line 228
def [](attr)
@attributes[attr.to_s]
end
|
#attributes ⇒ Object
220
221
222
|
# File 'lib/frozen_record/base.rb', line 220
def attributes
@attributes.dup
end
|
#id ⇒ Object
224
225
226
|
# File 'lib/frozen_record/base.rb', line 224
def id
self[self.class.primary_key]
end
|
#persisted? ⇒ Boolean
237
238
239
|
# File 'lib/frozen_record/base.rb', line 237
def persisted?
true
end
|
#to_key ⇒ Object
241
242
243
|
# File 'lib/frozen_record/base.rb', line 241
def to_key
[id]
end
|