Class: Swarm::HiveDweller
- Inherits:
-
Object
- Object
- Swarm::HiveDweller
show all
- Extended by:
- Enumerable
- Defined in:
- lib/swarm/hive_dweller.rb
Defined Under Namespace
Classes: RecordNotFoundError
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hive: Hive.default, **args) ⇒ HiveDweller
Returns a new instance of HiveDweller.
7
8
9
10
11
|
# File 'lib/swarm/hive_dweller.rb', line 7
def initialize(hive: Hive.default, **args)
@hive = hive
@changed_attributes = {}
set_attributes(args, record_changes: false)
end
|
Class Attribute Details
.associations ⇒ Object
Returns the value of attribute associations.
93
94
95
|
# File 'lib/swarm/hive_dweller.rb', line 93
def associations
@associations
end
|
.columns ⇒ Object
Returns the value of attribute columns.
93
94
95
|
# File 'lib/swarm/hive_dweller.rb', line 93
def columns
@columns
end
|
Instance Attribute Details
#hive ⇒ Object
Returns the value of attribute hive.
5
6
7
|
# File 'lib/swarm/hive_dweller.rb', line 5
def hive
@hive
end
|
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/swarm/hive_dweller.rb', line 5
def id
@id
end
|
Class Method Details
.all(hive: Hive.default, subtypes: true) ⇒ Object
189
190
191
192
193
|
# File 'lib/swarm/hive_dweller.rb', line 189
def all(hive: Hive.default, subtypes: true)
hive.storage.all_of_type(storage_type, subtypes: subtypes).map { |hsh|
hive.reify_from_hash(hsh.dup)
}
end
|
.create(hive: Hive.default, **args) ⇒ Object
147
148
149
|
# File 'lib/swarm/hive_dweller.rb', line 147
def create(hive: Hive.default, **args)
new(hive: hive, created_at: Time.now, **args).save
end
|
.each(hive: Hive.default, subtypes: true, &block) ⇒ Object
179
180
181
182
183
184
185
186
187
|
# File 'lib/swarm/hive_dweller.rb', line 179
def each(hive: Hive.default, subtypes: true, &block)
return to_enum(__method__, hive: hive, subtypes: subtypes) unless block_given?
ids(hive: hive).each do |id|
object = fetch(id, hive: hive)
if (subtypes && object.is_a?(self)) || object.class == self
yield object
end
end
end
|
.fetch(key, hive: Hive.default) ⇒ Object
170
171
172
173
|
# File 'lib/swarm/hive_dweller.rb', line 170
def fetch(key, hive: Hive.default)
hsh = hive.storage[storage_id_for_key(key)].dup
hive.reify_from_hash(hsh)
end
|
.ids(hive: Hive.default) ⇒ Object
175
176
177
|
# File 'lib/swarm/hive_dweller.rb', line 175
def ids(hive: Hive.default)
hive.storage.ids_for_type(storage_type)
end
|
.inherited(subclass) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/swarm/hive_dweller.rb', line 95
def inherited(subclass)
super
subclass.instance_variable_set(:@columns, [])
subclass.instance_variable_set(:@associations, [])
subclass.set_columns :updated_at, :created_at
end
|
.many_to_one(type, class_name: nil, key: nil) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/swarm/hive_dweller.rb', line 133
def many_to_one(type, class_name: nil, key: nil)
define_method(type) do
memo = instance_variable_get(:"@#{type}")
memo || begin
key ||= :"#{type}_id"
associated_id = self.send(key)
return nil unless associated_id
klass = Swarm::Support.constantize("#{class_name || type}")
instance_variable_set(:"@#{type}", klass.fetch(associated_id, :hive => hive))
end
end
@associations << type
end
|
.new_from_storage(**args) ⇒ Object
163
164
165
166
167
168
|
# File 'lib/swarm/hive_dweller.rb', line 163
def new_from_storage(**args)
id = args.delete(:id)
new(**args).tap { |instance|
instance.instance_variable_set(:@id, id)
}
end
|
.one_to_many(type, class_name: nil, foreign_key: nil) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/swarm/hive_dweller.rb', line 119
def one_to_many(type, class_name: nil, foreign_key: nil)
define_method(type) do
memo = instance_variable_get(:"@#{type}")
memo || begin
associations = hive.storage.load_associations(
type, owner: self, type: class_name || type, foreign_key: foreign_key
)
entities = associations.map { |association| hive.reify_from_hash(association) }
instance_variable_set(:"@#{type}", entities)
end
end
@associations << type
end
|
.set_columns(*args) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/swarm/hive_dweller.rb', line 102
def set_columns(*args)
args.each do |arg|
define_method("#{arg}=") { |value|
change_attribute(arg, value)
}
define_method(arg) {
val = instance_variable_get(:"@#{arg}")
if /_at$/.match(arg) && val.is_a?(String)
val = Time.parse(val)
end
val
}
end
@columns = @columns | args
end
|
.storage_id_for_key(key) ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/swarm/hive_dweller.rb', line 155
def storage_id_for_key(key)
if key.match(/^#{storage_type}\:/)
key
else
"#{storage_type}:#{key}"
end
end
|
.storage_type ⇒ Object
151
152
153
|
# File 'lib/swarm/hive_dweller.rb', line 151
def storage_type
name.split("::").last
end
|
Instance Method Details
#==(other) ⇒ Object
38
39
40
|
# File 'lib/swarm/hive_dweller.rb', line 38
def ==(other)
other.is_a?(self.class) && other.to_hash == to_hash
end
|
#attributes ⇒ Object
64
65
66
67
68
|
# File 'lib/swarm/hive_dweller.rb', line 64
def attributes
self.class.columns.each_with_object({}) { |col_name, hsh|
hsh[col_name.to_sym] = send(:"#{col_name}")
}
end
|
#change_attribute(key, value, record: true) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/swarm/hive_dweller.rb', line 31
def change_attribute(key, value, record: true)
if record
@changed_attributes[key] = [send(key), value]
end
instance_variable_set(:"@#{key}", value)
end
|
#changed? ⇒ Boolean
17
18
19
|
# File 'lib/swarm/hive_dweller.rb', line 17
def changed?
!@changed_attributes.empty?
end
|
#delete ⇒ Object
50
51
52
53
|
# File 'lib/swarm/hive_dweller.rb', line 50
def delete
storage.delete(storage_id)
self
end
|
#new? ⇒ Boolean
13
14
15
|
# File 'lib/swarm/hive_dweller.rb', line 13
def new?
id.nil?
end
|
#reload! ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/swarm/hive_dweller.rb', line 78
def reload!
hsh = hive.storage[storage_id]
self.class.columns.each do |column|
instance_variable_set(:"@#{column}", hsh[column.to_s])
end
self.class.associations.each do |type|
instance_variable_set(:"@#{type}", nil)
end
@changed_attributes = {}
self
end
|
#save ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/swarm/hive_dweller.rb', line 55
def save
if new? || changed?
@id ||= Swarm::Support.uuid_with_timestamp
storage[storage_id] = to_hash.merge(:updated_at => Time.now)
reload!
end
self
end
|
#set_attributes(args, record_changes: true) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/swarm/hive_dweller.rb', line 21
def set_attributes(args, record_changes: true)
unknown_arguments = args.keys - self.class.columns
unless unknown_arguments.empty?
raise ArgumentError, "unknown keywords: #{unknown_arguments.join(', ')}"
end
args.each do |key, value|
change_attribute(key, value, record: record_changes)
end
end
|
#storage ⇒ Object
46
47
48
|
# File 'lib/swarm/hive_dweller.rb', line 46
def storage
@hive.storage
end
|
#storage_id ⇒ Object
42
43
44
|
# File 'lib/swarm/hive_dweller.rb', line 42
def storage_id
self.class.storage_id_for_key(id)
end
|
#to_hash ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/swarm/hive_dweller.rb', line 70
def to_hash
hsh = {
:id => id,
:type => self.class.name
}
hsh.merge(attributes)
end
|