fire-model 0.0.12 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/fire-model.gemspec +2 -2
- data/lib/model/base.rb +9 -7
- data/lib/model/nested/base.rb +20 -5
- data/lib/model/nested/parent.rb +26 -20
- data/lib/model/nested/single.rb +9 -2
- data/spec/models/nested_models_spec.rb +44 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65203fc48f1b59dc053e3a1fd9c644eeeb64fa3c
|
4
|
+
data.tar.gz: 8ac11f7090972f18e29af3f053adb534521edad8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2459a91fd0f9c59d2cf266f1c9f8c6956f57440e21bc834164d054ecbc2ee30db9f0ab820564a275c5435fc5144dcb4f55fefd56afcc60c0b5b8015801d40ea9
|
7
|
+
data.tar.gz: f6263a5a8f4ebcf06e79a870924a16bafc9e05db557abb3abf13ed1837ffaf67558aae3f3b48a111f421921c937b4e7f53619ee6b6587fc56d867e1e84b73752
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.14
|
data/fire-model.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: fire-model 0.0.
|
5
|
+
# stub: fire-model 0.0.14 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "fire-model"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.14"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
data/lib/model/base.rb
CHANGED
@@ -62,10 +62,14 @@ module Fire
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def update(attrs)
|
65
|
-
|
65
|
+
set(attrs)
|
66
66
|
save
|
67
67
|
end
|
68
68
|
|
69
|
+
def set(attrs)
|
70
|
+
@table.merge!(attrs)
|
71
|
+
end
|
72
|
+
|
69
73
|
# Data Methods
|
70
74
|
|
71
75
|
def path_values
|
@@ -110,10 +114,6 @@ module Fire
|
|
110
114
|
self.to_h
|
111
115
|
end
|
112
116
|
|
113
|
-
def saving_data
|
114
|
-
data
|
115
|
-
end
|
116
|
-
|
117
117
|
def cache(key, &value)
|
118
118
|
@cache ||= {}
|
119
119
|
@cache[key] ||= value.call
|
@@ -215,10 +215,12 @@ module Fire
|
|
215
215
|
require_relative './querying/querying'
|
216
216
|
include Querying
|
217
217
|
|
218
|
+
require_relative './nested/parent'
|
219
|
+
include NestedParent
|
220
|
+
|
218
221
|
require_relative './nested/base'
|
219
222
|
require_relative './nested/single'
|
220
|
-
require_relative './nested/parent'
|
221
223
|
|
222
|
-
include NestedModel::Parent
|
223
224
|
end
|
225
|
+
|
224
226
|
end
|
data/lib/model/nested/base.rb
CHANGED
@@ -3,14 +3,28 @@ module Fire
|
|
3
3
|
class NestedModel < Model
|
4
4
|
non_shared_cattr_accessor :parent, :nested_options
|
5
5
|
|
6
|
+
def initialize(hash={}, parent_original={})
|
7
|
+
@parent_original = parent_original
|
8
|
+
super(hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
def sync_parent
|
12
|
+
@parent_original.merge!(saving_data)
|
13
|
+
end
|
14
|
+
|
6
15
|
def saving_data
|
7
16
|
return super() if nested_options.parent_values
|
8
17
|
|
9
|
-
self.class.parent.all_path_keys.each_with_object(
|
18
|
+
self.class.parent.all_path_keys.each_with_object(data.clone) do |k, res|
|
10
19
|
res.delete(k)
|
11
20
|
end
|
12
21
|
end
|
13
22
|
|
23
|
+
def save
|
24
|
+
sync_parent
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
14
28
|
def nested_options
|
15
29
|
self.class.nested_options
|
16
30
|
end
|
@@ -48,10 +62,11 @@ module Fire
|
|
48
62
|
|
49
63
|
def folder_content(parent)
|
50
64
|
levels_count = (path_keys || []).count
|
51
|
-
|
52
|
-
objects.
|
53
|
-
|
54
|
-
|
65
|
+
nested_folder = parent.send(folder) || {}
|
66
|
+
objects = self.down_levels(nested_folder, levels_count)
|
67
|
+
objects.map{|parent_original|
|
68
|
+
full_data = parent_original.clone.merge(parent.path_data)
|
69
|
+
new(full_data, parent_original)
|
55
70
|
}
|
56
71
|
end
|
57
72
|
|
data/lib/model/nested/parent.rb
CHANGED
@@ -1,36 +1,42 @@
|
|
1
1
|
module Fire
|
2
|
-
|
3
|
-
|
4
|
-
extend ActiveSupport::Concern
|
2
|
+
module NestedParent
|
3
|
+
extend ActiveSupport::Concern
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
included do
|
6
|
+
non_shared_cattr_accessor :assigned_nested_models
|
7
|
+
end
|
9
8
|
|
10
|
-
|
9
|
+
def saving_data
|
10
|
+
self.class.nested_models.map(&:folder).each{|folder|
|
11
|
+
nested_folder = send("nested_#{folder}")
|
12
|
+
nested_folder.is_a?(Array) ? nested_folder.each(&:sync_parent) : nested_folder.sync_parent
|
13
|
+
}
|
14
|
+
data
|
15
|
+
end
|
11
16
|
|
12
|
-
|
13
|
-
self.assigned_nested_models ||= []
|
14
|
-
self.assigned_nested_models << nested_model
|
17
|
+
module ClassMethods
|
15
18
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
nested_model.folder_content(self)
|
20
|
-
end
|
21
|
-
end
|
19
|
+
def has_nested(nested_model)
|
20
|
+
self.assigned_nested_models ||= []
|
21
|
+
self.assigned_nested_models << nested_model
|
22
22
|
|
23
|
-
|
24
|
-
|
23
|
+
folder = nested_model.folder
|
24
|
+
define_method "nested_#{folder}" do
|
25
|
+
self.cache(folder) do
|
26
|
+
nested_model.folder_content(self)
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
|
29
|
-
self.
|
30
|
+
define_method "add_to_#{folder}" do |object|
|
31
|
+
nested_model.create(object.merge(self.path_data))
|
30
32
|
end
|
33
|
+
end
|
31
34
|
|
35
|
+
def nested_models
|
36
|
+
self.assigned_nested_models || []
|
32
37
|
end
|
33
38
|
|
34
39
|
end
|
35
40
|
end
|
36
41
|
end
|
42
|
+
|
data/lib/model/nested/single.rb
CHANGED
@@ -9,8 +9,9 @@ module Fire
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def folder_content(parent)
|
12
|
-
|
13
|
-
|
12
|
+
init_empty_parent_object(parent)
|
13
|
+
parent_original = parent.send(folder)
|
14
|
+
new(parent_original.clone.merge(parent.path_data), parent_original)
|
14
15
|
end
|
15
16
|
|
16
17
|
protected
|
@@ -23,6 +24,12 @@ module Fire
|
|
23
24
|
name
|
24
25
|
end
|
25
26
|
|
27
|
+
def init_empty_parent_object(parent)
|
28
|
+
unless parent.send(folder)
|
29
|
+
parent.send("#{folder}=", {})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
26
33
|
end
|
27
34
|
|
28
35
|
class PathKeysNotSupported < FireModelError
|
@@ -114,6 +114,42 @@ describe 'Nested Models' do
|
|
114
114
|
'name'=>'Google',
|
115
115
|
'state'=>'CA'}}}}}
|
116
116
|
)
|
117
|
+
|
118
|
+
employee = google.reload.nested_employees.first
|
119
|
+
|
120
|
+
employee.full_name = 'Sergey Brin'
|
121
|
+
employee.position = 'CEO'
|
122
|
+
|
123
|
+
google.save
|
124
|
+
|
125
|
+
expect(current_data).to eq(
|
126
|
+
{'Organization'=>
|
127
|
+
{'usa'=>
|
128
|
+
{'ca'=>
|
129
|
+
{'apple'=>
|
130
|
+
{'country'=>'USA',
|
131
|
+
'employees'=>
|
132
|
+
{'hq'=>
|
133
|
+
{tim.id=>
|
134
|
+
{'department'=>'HQ',
|
135
|
+
'full_name'=>'Tim Cook',
|
136
|
+
'id'=>tim.id,
|
137
|
+
'position'=>'CEO'}}},
|
138
|
+
'name'=>'Apple',
|
139
|
+
'state'=>'CA'},
|
140
|
+
'google'=>
|
141
|
+
{'country'=>'USA',
|
142
|
+
'employees'=>
|
143
|
+
{'research'=>
|
144
|
+
{larry.id=>
|
145
|
+
{'department'=>'Research',
|
146
|
+
'full_name'=>'Sergey Brin',
|
147
|
+
'id'=>larry.id,
|
148
|
+
'position'=>'CEO'}}},
|
149
|
+
'name'=>'Google',
|
150
|
+
'state'=>'CA'}}}}}
|
151
|
+
)
|
152
|
+
|
117
153
|
end
|
118
154
|
|
119
155
|
context 'Nested Models Types' do
|
@@ -162,10 +198,18 @@ describe 'Nested Models' do
|
|
162
198
|
expect(zaporozhets.nested_engine.code).to eq('MeMZ-777')
|
163
199
|
expect(zaporozhets.reload.nested_engine.code).to eq('MeMZ-966')
|
164
200
|
|
201
|
+
|
165
202
|
# nested association saving
|
166
203
|
zap2 = Car.take(manufacturer: 'Zaporozhets', model: 'ZAZ-965', car_class: 'Mini', id: car.id)
|
167
204
|
zap2.nested_engine.update(code: 'MeMZ-555')
|
168
205
|
expect(zaporozhets.nested_engine.reload.code).to eq('MeMZ-555')
|
206
|
+
|
207
|
+
# saving nested models with a parent
|
208
|
+
zap3 = Car.take(manufacturer: 'Zaporozhets', model: 'ZAZ-965', car_class: 'Mini', id: car.id)
|
209
|
+
zap3.nested_engine.code = 'MeMZ-1111'
|
210
|
+
|
211
|
+
zap3.save
|
212
|
+
expect(zap3.reload.nested_engine.code).to eq('MeMZ-1111')
|
169
213
|
end
|
170
214
|
|
171
215
|
it 'should allow to declare nested models with all *parent values* duplicated' do
|