fire-model 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -6
- data/VERSION +1 -1
- data/fire-model.gemspec +3 -3
- data/lib/model/nested/base.rb +29 -2
- data/lib/model/nested/single.rb +18 -0
- data/spec/models/nested_models_spec.rb +28 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fdb250be82593bbccbf6d91e9f68d48cb0a5e66
|
4
|
+
data.tar.gz: 00c3883855b7cceca2cae97d5fd08416f7ec9524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90e0dd13ee9b87a69bcda662958df06308607c648907c9f67a96cef3e6b23520f88ceecafaf8e486d4db1ceda561a0a710e71240924df1e11d4eed4690843be
|
7
|
+
data.tar.gz: 658af712fb067d30cfacfb60695fe3637365bc2970fd5dc8cf6937b10fd5a6a9fd3209dc3675a79adbf69ff2cc340124c024c4784c574614b8c8e5589c25492b
|
data/README.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# fire-model - REST wrapper for Firebase.
|
2
2
|
|
3
|
+
Install gem
|
4
|
+
```
|
5
|
+
gem install fire-model
|
6
|
+
```
|
3
7
|
|
4
|
-
Setup Firebase
|
8
|
+
Setup Firebase. (If you are using Rails create a file `Rails.root/config/initializers/fire_model.rb` and put next line there)
|
5
9
|
```ruby
|
10
|
+
require 'fire-model'
|
6
11
|
Fire.setup(firebase_path: 'https://some-test-777.firebaseio.com')
|
7
12
|
```
|
8
13
|
|
@@ -101,8 +106,8 @@ Point.all.map(&:value)
|
|
101
106
|
Create Nested Models
|
102
107
|
```ruby
|
103
108
|
class Organization < Fire::Model
|
104
|
-
has_path_keys :country, :state
|
105
109
|
set_id_key(:name)
|
110
|
+
has_path_keys :country, :state
|
106
111
|
end
|
107
112
|
|
108
113
|
class Employee < Fire::NestedModel
|
@@ -176,7 +181,8 @@ car = Car.create(manufacturer: 'Zaporozhets', model: 'ZAZ-965', car_class: 'Mini
|
|
176
181
|
engine: { code: 'MeMZ-966' })
|
177
182
|
|
178
183
|
zaporozhets = Car.take(manufacturer: 'Zaporozhets', model: 'ZAZ-965', car_class: 'Mini', id: car.id)
|
179
|
-
|
184
|
+
zaporozhets.nested_engine.code
|
185
|
+
=> 'MeMZ-966'
|
180
186
|
|
181
187
|
Fire.tree
|
182
188
|
=> {
|
@@ -209,13 +215,13 @@ zaporozhets.nested_engine.reload.code
|
|
209
215
|
Nested Models with Parent`s values
|
210
216
|
```ruby
|
211
217
|
class House < Fire::Model
|
212
|
-
has_path_keys :country, :city, :street
|
213
218
|
set_id_key(:house_number)
|
219
|
+
has_path_keys :country, :city, :street
|
214
220
|
end
|
215
221
|
|
216
222
|
class Room < Fire::NestedModel
|
217
|
-
nested_in House, parent_values: true
|
218
223
|
set_id_key(:number)
|
224
|
+
nested_in House, parent_values: true
|
219
225
|
has_path_keys :floor
|
220
226
|
end
|
221
227
|
|
@@ -227,7 +233,8 @@ house.add_to_rooms(floor: 150, number: '2A')
|
|
227
233
|
|
228
234
|
|
229
235
|
rooms = house.reload.nested_rooms
|
230
|
-
|
236
|
+
rooms.map(&:number)
|
237
|
+
=> [ '1A', '2A' ]
|
231
238
|
|
232
239
|
Fire.tree
|
233
240
|
=> {'House'=>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.16
|
data/fire-model.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
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.16 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.16"
|
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"]
|
13
13
|
s.authors = ["Vitaly Tarasenko"]
|
14
|
-
s.date = "2015-06-
|
14
|
+
s.date = "2015-06-24"
|
15
15
|
s.description = "You can define your Firebase models, set collection names, CRUD your data. "
|
16
16
|
s.email = "vetal.tarasenko@gmail.com"
|
17
17
|
s.extra_rdoc_files = [
|
data/lib/model/nested/base.rb
CHANGED
@@ -29,6 +29,16 @@ module Fire
|
|
29
29
|
self.class.nested_options
|
30
30
|
end
|
31
31
|
|
32
|
+
def self.id_key
|
33
|
+
self.id_key_name || "#{self.name.demodulize.singularize.dasherize.downcase}_id".to_sym
|
34
|
+
end
|
35
|
+
|
36
|
+
def set_id_key(value)
|
37
|
+
raise ParentModelNotSetError.new(self) unless self.parent
|
38
|
+
validate_id_key!
|
39
|
+
super(value)
|
40
|
+
end
|
41
|
+
|
32
42
|
class << self
|
33
43
|
def in_collection(name)
|
34
44
|
raise CollectionIsSetError.new(self)
|
@@ -46,6 +56,7 @@ module Fire
|
|
46
56
|
self.parent = parent
|
47
57
|
self.nested_options = OpenStruct.new(options)
|
48
58
|
self.parent.has_nested(self)
|
59
|
+
validate_id_key!
|
49
60
|
end
|
50
61
|
|
51
62
|
def own_path_keys
|
@@ -81,7 +92,11 @@ module Fire
|
|
81
92
|
protected
|
82
93
|
|
83
94
|
def default_folder_name
|
84
|
-
name.pluralize
|
95
|
+
name.demodulize.pluralize.dasherize
|
96
|
+
end
|
97
|
+
|
98
|
+
def validate_id_key!
|
99
|
+
raise DuplicatedIdKeyError.new(self.id_key, self.parent) if self.parent.all_path_keys.include?(id_key)
|
85
100
|
end
|
86
101
|
end
|
87
102
|
|
@@ -95,9 +110,21 @@ module Fire
|
|
95
110
|
|
96
111
|
class DuplicatedParentPathKeyError < InvalidPathKeyError
|
97
112
|
def initialize(key, parent)
|
98
|
-
message =
|
113
|
+
message = error_message(key, parent)
|
99
114
|
super(key, message)
|
100
115
|
end
|
116
|
+
|
117
|
+
def error_message(key, parent)
|
118
|
+
"Key '#{key}' is duplicated in a Parent Model '#{parent}'"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
class DuplicatedIdKeyError < DuplicatedParentPathKeyError
|
123
|
+
|
124
|
+
def error_message(key, parent)
|
125
|
+
"ID Key '#{key}' is duplicated in a Parent Model '#{parent}'. Use 'set_key_id' method to define a specific ID Key"
|
126
|
+
end
|
127
|
+
|
101
128
|
end
|
102
129
|
|
103
130
|
class ParentModelNotSetError < FireModelError
|
data/lib/model/nested/single.rb
CHANGED
@@ -14,6 +14,14 @@ module Fire
|
|
14
14
|
new(parent_original.clone.merge(parent.path_data), parent_original)
|
15
15
|
end
|
16
16
|
|
17
|
+
def id_key
|
18
|
+
parent.id_key
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_id_key(value)
|
22
|
+
raise IdKeyNotSupported.new(self)
|
23
|
+
end
|
24
|
+
|
17
25
|
protected
|
18
26
|
|
19
27
|
def default_path_keys
|
@@ -30,6 +38,16 @@ module Fire
|
|
30
38
|
end
|
31
39
|
end
|
32
40
|
|
41
|
+
def validate_id_key!
|
42
|
+
true
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
class IdKeyNotSupported < FireModelError
|
48
|
+
def initialize(single_nested_model)
|
49
|
+
super("Single Model #{single_nested_model} don't support own ID key.")
|
50
|
+
end
|
33
51
|
end
|
34
52
|
|
35
53
|
class PathKeysNotSupported < FireModelError
|
@@ -26,6 +26,7 @@ describe 'Nested Models' do
|
|
26
26
|
expect(Organization.nested_models).to be_empty
|
27
27
|
|
28
28
|
class Employee < Fire::NestedModel
|
29
|
+
set_id_key(:id)
|
29
30
|
nested_in Organization
|
30
31
|
has_path_keys :department
|
31
32
|
end
|
@@ -161,6 +162,7 @@ describe 'Nested Models' do
|
|
161
162
|
end
|
162
163
|
|
163
164
|
class Engine < Fire::SingleNestedModel
|
165
|
+
|
164
166
|
nested_in Car
|
165
167
|
end
|
166
168
|
|
@@ -265,6 +267,32 @@ describe 'Nested Models' do
|
|
265
267
|
end
|
266
268
|
end
|
267
269
|
|
270
|
+
context 'Relations' do
|
271
|
+
|
272
|
+
before :all do
|
273
|
+
class Post < Fire::Model
|
274
|
+
has_path_keys :category
|
275
|
+
|
276
|
+
class Comment < Fire::NestedModel
|
277
|
+
nested_in Post
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'should be associated' do
|
283
|
+
expect(Post.id_key).to eq(:id)
|
284
|
+
expect(Post::Comment.id_key).to eq(:comment_id)
|
285
|
+
expect(Post::Comment.folder).to eq('comments')
|
286
|
+
|
287
|
+
post = Post.create(category: 'default')
|
288
|
+
post.add_to_comments(text: 'hello')
|
289
|
+
post.add_to_comments(text: 'hey')
|
290
|
+
|
291
|
+
expect(post.reload.nested_comments.map(&:text).sort).to eq(%w{ hello hey }.sort)
|
292
|
+
end
|
293
|
+
|
294
|
+
end
|
295
|
+
|
268
296
|
context 'Restrictions' do
|
269
297
|
before :each do
|
270
298
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fire-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitaly Tarasenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tarvit-helpers
|