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
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
142
143
144
|
# File 'lib/frozen_record/base.rb', line 142
def initialize(attrs = {})
@attributes = attrs.stringify_keys
end
|
Class Attribute Details
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
51
52
53
|
# File 'lib/frozen_record/base.rb', line 51
def abstract_class
@abstract_class
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
140
141
142
|
# File 'lib/frozen_record/base.rb', line 140
def attributes
@attributes
end
|
Class Method Details
.abstract_class? ⇒ Boolean
53
54
55
|
# File 'lib/frozen_record/base.rb', line 53
def abstract_class?
defined?(@abstract_class) && @abstract_class
end
|
.current_scope ⇒ Object
Also known as:
all
57
58
59
|
# File 'lib/frozen_record/base.rb', line 57
def current_scope
store[:scope] ||= Scope.new(self)
end
|
.current_scope=(scope) ⇒ Object
62
63
64
|
# File 'lib/frozen_record/base.rb', line 62
def current_scope=(scope)
store[:scope] = scope
end
|
.eager_load! ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/frozen_record/base.rb', line 81
def eager_load!
if auto_reloading
raise RuntimeError, "There is no point eager loading a FrozenRecord if auto_reloading is enabled!"
end
return if abstract_class?
load_records
end
|
.file_path ⇒ Object
69
70
71
72
|
# File 'lib/frozen_record/base.rb', line 69
def file_path
raise ArgumentError, "You must define `#{name}.base_path`" unless base_path
File.join(base_path, backend.filename(name))
end
|
.load_records ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/frozen_record/base.rb', line 91
def load_records
if auto_reloading && file_changed?
@records = nil
undefine_attribute_methods
end
@records ||= begin
records = backend.load(file_path)
define_attribute_methods(list_attributes(records))
records.map(&method(:new)).freeze
end
end
|
.respond_to_missing?(name) ⇒ Boolean
74
75
76
77
78
79
|
# File 'lib/frozen_record/base.rb', line 74
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
|
Instance Method Details
#==(other) ⇒ Object
155
156
157
|
# File 'lib/frozen_record/base.rb', line 155
def ==(other)
super || other.is_a?(self.class) && other.id == id
end
|
#[](attr) ⇒ Object
Also known as:
attribute
150
151
152
|
# File 'lib/frozen_record/base.rb', line 150
def [](attr)
@attributes[attr.to_s]
end
|
#id ⇒ Object
146
147
148
|
# File 'lib/frozen_record/base.rb', line 146
def id
self[primary_key]
end
|
#persisted? ⇒ Boolean
159
160
161
|
# File 'lib/frozen_record/base.rb', line 159
def persisted?
true
end
|
#to_key ⇒ Object
163
164
165
|
# File 'lib/frozen_record/base.rb', line 163
def to_key
[id]
end
|