gdatastore_mapper 0.1.1 → 0.1.2bata
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +73 -6
- data/lib/gdatastore_mapper.rb +0 -1
- data/lib/gdatastore_mapper/associations.rb +24 -4
- data/lib/gdatastore_mapper/associations/has_many.rb +28 -0
- data/lib/gdatastore_mapper/base.rb +14 -7
- data/lib/gdatastore_mapper/relation.rb +25 -2
- data/lib/gdatastore_mapper/scoping.rb +32 -3
- data/lib/gdatastore_mapper/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96cdd7dfe7d4ff1f1770ae525b2e142bd12ca1d3
|
4
|
+
data.tar.gz: 4c25acbe84f26e5480ddf5d42e80e25bde7080a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d8aef851f6ed9db4960e0eb3836f25fe3726a5365266fa9d6ec9a0522e9b14aa0168220643677019ac8c3b7a650def18c2f75595d2b61acce76069a3bb1283d
|
7
|
+
data.tar.gz: cc1489a449780b14b03bbd42d029c8f9f06f4fb074ef6ddbf8bfd7b3fb99717fc61f7bfe04af050ee6b55586a423e947f85dadaee9981536d5e76b9a86abcb60
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# GdatastoreMapper
|
2
2
|
|
3
|
-
GdatastoreMapper is a mapper framework for Google Cloud Datastore in Ruby / Ruby on Rails
|
3
|
+
GdatastoreMapper is a mapper framework for Google Cloud Datastore in Ruby / Ruby on Rails.
|
4
4
|
Once you install GdatastoreMapper you can use Google Cloud Datastore like ActiveRecord.
|
5
5
|
|
6
6
|
## Installation
|
@@ -44,8 +44,8 @@ test:
|
|
44
44
|
|
45
45
|
Only 2 things you need to do.
|
46
46
|
|
47
|
-
1 To include GdatastoreMapper
|
48
|
-
2 To set attr_accessor as column
|
47
|
+
1. To include GdatastoreMapper
|
48
|
+
2. To set attr_accessor as column
|
49
49
|
|
50
50
|
That's it!
|
51
51
|
|
@@ -69,10 +69,10 @@ book = Book.new(title: 'Harry Potter')
|
|
69
69
|
book.save
|
70
70
|
```
|
71
71
|
```
|
72
|
-
Book.create(title: 'Harry Potter'
|
72
|
+
Book.create(title: 'Harry Potter')
|
73
73
|
```
|
74
74
|
```
|
75
|
-
book.update(title: 'Harry Potter 2'
|
75
|
+
book.update(title: 'Harry Potter 2')
|
76
76
|
```
|
77
77
|
```
|
78
78
|
book.delete
|
@@ -82,17 +82,84 @@ book.delete
|
|
82
82
|
|
83
83
|
```
|
84
84
|
Book.where(title: 'Harry Potter')
|
85
|
+
=> [#<Book:0x00 @created_at=2017-04-08 21:22:31 +0200, @title="Harry Potter",
|
86
|
+
@id=70, @updated_at=2017-04-08 21:22:31 +0200>]
|
85
87
|
```
|
86
88
|
```
|
87
89
|
Book.find(12)
|
90
|
+
=> #<Book:0x00 @created_at=2017-04-07 10:03:54 +0200, @title="Harry Potter",
|
91
|
+
@id=12, @updated_at=2017-04-07 22:57:57 +0200>
|
88
92
|
```
|
89
93
|
```
|
90
94
|
Book.find_by(title: 'Harry Potter')
|
95
|
+
=> #<Book:0x00 @created_at ....
|
91
96
|
```
|
92
97
|
```
|
93
98
|
Book.order(title: :asc)
|
99
|
+
=> [#<Book:0x00 @created_at .... ]
|
94
100
|
```
|
101
|
+
```
|
102
|
+
Book.first
|
103
|
+
=> #<Book:0x00 @created_at ....
|
104
|
+
```
|
105
|
+
```
|
106
|
+
Book.last
|
107
|
+
=> #<Book:0x00 @created_at ....
|
108
|
+
```
|
109
|
+
```
|
110
|
+
Book.count
|
111
|
+
=> 100
|
112
|
+
```
|
113
|
+
```
|
114
|
+
Book.all
|
115
|
+
=> [#<Book:0x00 @created_at .... ]
|
116
|
+
```
|
117
|
+
|
118
|
+
## Associations
|
119
|
+
|
120
|
+
example of one to many relationship
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
class Book
|
124
|
+
include GdatastoreMapper::Base
|
125
|
+
|
126
|
+
attr_accessor :title
|
127
|
+
|
128
|
+
belongs_to :author
|
129
|
+
end
|
130
|
+
```
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
class Author
|
134
|
+
include GdatastoreMapper::Base
|
135
|
+
|
136
|
+
attr_accessor :name
|
95
137
|
|
138
|
+
has_many :books
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
books.create
|
143
|
+
```
|
144
|
+
j_k_rolling = Author.create(name: 'J K Rolling')
|
145
|
+
harry_poter = j_k_rolling.books.create(title: 'Harry Poter')
|
146
|
+
harry_poter_2 = j_k_rolling.books.create(title: 'Harry Poter 2')
|
147
|
+
```
|
148
|
+
books
|
149
|
+
```
|
150
|
+
j_k_rolling.books
|
151
|
+
=> [#<Book:0x00 @created_at .... ]
|
152
|
+
```
|
153
|
+
|
154
|
+
books.count
|
155
|
+
```
|
156
|
+
j_k_rolling.books.count
|
157
|
+
=> 2
|
158
|
+
```
|
159
|
+
```
|
160
|
+
harry_poter.author
|
161
|
+
=> [#<Author:0x00 @created_at .... ]
|
162
|
+
```
|
96
163
|
|
97
164
|
## Development
|
98
165
|
|
@@ -102,7 +169,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
102
169
|
|
103
170
|
## Contributing
|
104
171
|
|
105
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
172
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shinyaK14/gdatastore_mapper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
106
173
|
|
107
174
|
|
108
175
|
## License
|
data/lib/gdatastore_mapper.rb
CHANGED
@@ -1,18 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
require "google/cloud"
|
3
|
+
require "gdatastore_mapper/relation"
|
4
|
+
require "gdatastore_mapper/associations/has_many"
|
2
5
|
|
3
6
|
module GdatastoreMapper
|
4
7
|
module Associations
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
def self.included(klass)
|
11
|
+
klass.include Associations
|
12
|
+
end
|
5
13
|
|
6
14
|
def has_many models
|
15
|
+
self.class_eval("attr_accessor :#{models.to_s + '_id'}")
|
7
16
|
|
17
|
+
define_method models do
|
18
|
+
association = GdatastoreMapper::Associations::HasMany.new(self, models)
|
19
|
+
belongings = GdatastoreMapper::Relation.new(self, association)
|
20
|
+
if self.attributes.include?(id_ models) && !self.send(id_ models).nil?
|
21
|
+
self.send(id_ models).each do |id|
|
22
|
+
belongings << (models.to_s.classify.constantize.find id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
belongings
|
26
|
+
end
|
8
27
|
end
|
9
28
|
|
10
29
|
def belongs_to model
|
11
|
-
|
12
|
-
@belongs = model
|
30
|
+
self.class_eval("attr_accessor :#{model.to_s + '_id'}")
|
13
31
|
|
14
|
-
define_method
|
15
|
-
|
32
|
+
define_method model do
|
33
|
+
if self.attributes.include?(id_ model)
|
34
|
+
model.to_s.classify.constantize.find self.send(id_ model)
|
35
|
+
end
|
16
36
|
end
|
17
37
|
end
|
18
38
|
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module GdatastoreMapper
|
2
|
+
module Associations
|
3
|
+
class HasMany
|
4
|
+
|
5
|
+
attr_accessor :owner, :belonging
|
6
|
+
|
7
|
+
def initialize(owner, belonging)
|
8
|
+
@owner = owner
|
9
|
+
@belonging = belonging
|
10
|
+
end
|
11
|
+
|
12
|
+
def belonging_klass
|
13
|
+
@belonging.to_s.classify.constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
def owner_attributes
|
17
|
+
{
|
18
|
+
(@owner.class.to_s.underscore + '_id') => @owner.id
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def belonging_id
|
23
|
+
@belonging.to_s + '_id'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -54,17 +54,24 @@ module GdatastoreMapper
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def to_entity
|
57
|
-
|
58
|
-
entity.key = Google::Cloud::Datastore::Key.new self.class.to_s, id
|
59
|
-
|
57
|
+
entity_timestamp
|
60
58
|
attributes.each do |attribute|
|
61
|
-
entity[attribute] = self.send attribute if self.send attribute
|
59
|
+
@entity[attribute] = self.send attribute if self.send attribute
|
62
60
|
end
|
61
|
+
@entity
|
62
|
+
end
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
private
|
65
|
+
|
66
|
+
def entity_timestamp
|
67
|
+
@entity = Google::Cloud::Datastore::Entity.new
|
68
|
+
@entity.key = Google::Cloud::Datastore::Key.new self.class.to_s, id
|
69
|
+
@entity['created_at'] = id ? self.created_at : Time.zone.now
|
70
|
+
@entity["updated_at"] = Time.zone.now
|
67
71
|
end
|
68
72
|
|
73
|
+
def id_ model
|
74
|
+
(model.to_s + '_id').to_sym
|
75
|
+
end
|
69
76
|
end
|
70
77
|
end
|
@@ -1,5 +1,28 @@
|
|
1
1
|
module GdatastoreMapper
|
2
|
-
class Relation
|
3
|
-
|
2
|
+
class Relation < Array
|
3
|
+
def initialize(klass, association)
|
4
|
+
@klass = klass
|
5
|
+
@association = association
|
6
|
+
end
|
7
|
+
|
8
|
+
def create attributes
|
9
|
+
belonging = create_belonging attributes
|
10
|
+
update_owner belonging
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def create_belonging attributes
|
16
|
+
belonging_attr = attributes.merge(@association.owner_attributes)
|
17
|
+
@association.belonging_klass.create(belonging_attr)
|
18
|
+
end
|
19
|
+
|
20
|
+
def update_owner belonging
|
21
|
+
existing_ids = @association.owner.send(@association.belonging_id)
|
22
|
+
existing_ids = [] if existing_ids.nil?
|
23
|
+
owner_attr = {}
|
24
|
+
owner_attr[@association.belonging_id] = (existing_ids << belonging.id)
|
25
|
+
@association.owner.update(owner_attr)
|
26
|
+
end
|
4
27
|
end
|
5
28
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
require "google/cloud"
|
2
|
+
require "gdatastore_mapper/relation"
|
2
3
|
|
3
4
|
module GdatastoreMapper
|
4
5
|
module Scoping
|
5
6
|
|
6
7
|
def where condition
|
7
8
|
return nil unless condition.is_a?(Hash)
|
8
|
-
|
9
|
+
dataset_run(where_query condition)
|
9
10
|
end
|
10
11
|
|
11
12
|
def find id
|
@@ -20,11 +21,37 @@ module GdatastoreMapper
|
|
20
21
|
where(condition)&.first
|
21
22
|
end
|
22
23
|
|
24
|
+
def find_or_create condition
|
25
|
+
return nil unless condition.is_a?(Hash)
|
26
|
+
if record = where(condition)&.first
|
27
|
+
record
|
28
|
+
else
|
29
|
+
create condition
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
23
33
|
def order condition
|
24
34
|
return nil unless condition.is_a?(Hash)
|
25
35
|
dataset_run(order_query condition)
|
26
36
|
end
|
27
37
|
|
38
|
+
def all
|
39
|
+
order(created_at: :asc)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def first
|
44
|
+
all.first
|
45
|
+
end
|
46
|
+
|
47
|
+
def last
|
48
|
+
all.last
|
49
|
+
end
|
50
|
+
|
51
|
+
def count
|
52
|
+
all.count
|
53
|
+
end
|
54
|
+
|
28
55
|
private
|
29
56
|
|
30
57
|
def where_query condition
|
@@ -47,9 +74,11 @@ module GdatastoreMapper
|
|
47
74
|
|
48
75
|
def dataset_run query
|
49
76
|
entities = GdatastoreMapper::Session.dataset.run query
|
50
|
-
|
51
|
-
|
77
|
+
result = GdatastoreMapper::Relation.new(self, nil)
|
78
|
+
entities.each do |entity|
|
79
|
+
result << (from_entity entity)
|
52
80
|
end
|
81
|
+
result
|
53
82
|
end
|
54
83
|
end
|
55
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gdatastore_mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2bata
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shinya Kitamura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- gdatastore_mapper.gemspec
|
87
87
|
- lib/gdatastore_mapper.rb
|
88
88
|
- lib/gdatastore_mapper/associations.rb
|
89
|
+
- lib/gdatastore_mapper/associations/has_many.rb
|
89
90
|
- lib/gdatastore_mapper/base.rb
|
90
91
|
- lib/gdatastore_mapper/persistence.rb
|
91
92
|
- lib/gdatastore_mapper/relation.rb
|
@@ -107,9 +108,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
108
|
version: '0'
|
108
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
110
|
requirements:
|
110
|
-
- - "
|
111
|
+
- - ">"
|
111
112
|
- !ruby/object:Gem::Version
|
112
|
-
version:
|
113
|
+
version: 1.3.1
|
113
114
|
requirements: []
|
114
115
|
rubyforge_project:
|
115
116
|
rubygems_version: 2.6.11
|