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.
141
142
143
|
# File 'lib/frozen_record/base.rb', line 141
def initialize(attrs = {})
@attributes = attrs.stringify_keys
end
|
Class Attribute Details
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
47
48
49
|
# File 'lib/frozen_record/base.rb', line 47
def abstract_class
@abstract_class
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
139
140
141
|
# File 'lib/frozen_record/base.rb', line 139
def attributes
@attributes
end
|
Class Method Details
.abstract_class? ⇒ Boolean
49
50
51
|
# File 'lib/frozen_record/base.rb', line 49
def abstract_class?
defined?(@abstract_class) && @abstract_class
end
|
.current_scope ⇒ Object
Also known as:
all
53
54
55
|
# File 'lib/frozen_record/base.rb', line 53
def current_scope
store[:scope] ||= Scope.new(self)
end
|
.current_scope=(scope) ⇒ Object
58
59
60
|
# File 'lib/frozen_record/base.rb', line 58
def current_scope=(scope)
store[:scope] = scope
end
|
.eager_load! ⇒ Object
77
78
79
80
81
82
83
84
85
|
# File 'lib/frozen_record/base.rb', line 77
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
65
66
67
68
|
# File 'lib/frozen_record/base.rb', line 65
def file_path
raise ArgumentError, "You must define `#{name}.base_path`" unless base_path
File.join(base_path, "#{name.underscore.pluralize}.yml")
end
|
.load_records ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/frozen_record/base.rb', line 87
def load_records
if auto_reloading && file_changed?
@records = nil
undefine_attribute_methods
end
@records ||= begin
yml_erb_data = File.read(file_path)
yml_data = ERB.new(yml_erb_data).result
records = YAML.load(yml_data) || []
define_attribute_methods(list_attributes(records))
records.map(&method(:new)).freeze
end
end
|
.respond_to_missing?(name) ⇒ Boolean
70
71
72
73
74
75
|
# File 'lib/frozen_record/base.rb', line 70
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
154
155
156
|
# File 'lib/frozen_record/base.rb', line 154
def ==(other)
super || other.is_a?(self.class) && other.id == id
end
|
#[](attr) ⇒ Object
Also known as:
attribute
149
150
151
|
# File 'lib/frozen_record/base.rb', line 149
def [](attr)
@attributes[attr.to_s]
end
|
#id ⇒ Object
145
146
147
|
# File 'lib/frozen_record/base.rb', line 145
def id
self[primary_key]
end
|
#persisted? ⇒ Boolean
158
159
160
|
# File 'lib/frozen_record/base.rb', line 158
def persisted?
true
end
|
#to_key ⇒ Object
162
163
164
|
# File 'lib/frozen_record/base.rb', line 162
def to_key
[id]
end
|