gdatastore_mapper 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/README.md +74 -4
- data/gdatastore_mapper.gemspec +1 -1
- data/lib/gdatastore_mapper/associations.rb +20 -0
- data/lib/gdatastore_mapper/base.rb +70 -0
- data/lib/gdatastore_mapper/persistence.rb +35 -0
- data/lib/gdatastore_mapper/relation.rb +5 -0
- data/lib/gdatastore_mapper/scoping.rb +55 -0
- data/lib/gdatastore_mapper/session.rb +18 -0
- data/lib/gdatastore_mapper/version.rb +1 -1
- data/lib/gdatastore_mapper.rb +6 -3
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 252614e80440189db611c059b54b585ef35fb463
|
4
|
+
data.tar.gz: 327bfa042abefc8ab140922550911efc6b156806
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbd509bfd392f50b11e5bbf1c331932915b8d2984637c20d16788be5bafa1a718493c777a7491ecda8fdea11a3a103c2169866430595a2a9c74fdb345725f221
|
7
|
+
data.tar.gz: ffa79b0e909e5912fad1fbf1d2f3977934b09a75f8148adf652877467fc18ab863cbcde423f8abbd88c738f2daa923256fc416480dc0e77806bd72720c605bbb
|
data/Gemfile
CHANGED
@@ -3,6 +3,10 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in gdatastore_mapper.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
+
gem 'activemodel', '~> 5.0.1'
|
7
|
+
gem 'activesupport', '~> 5.0.1'
|
8
|
+
gem 'rake'
|
9
|
+
|
6
10
|
group :test do
|
7
11
|
gem 'byebug', platform: :mri
|
8
12
|
gem 'factory_girl_rails', require: false
|
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# GdatastoreMapper
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
GdatastoreMapper is a mapper framework for Google Cloud Datastore in Ruby / Ruby on Rails
|
4
|
+
Once you install GdatastoreMapper you can use Google Cloud Datastore like ActiveRecord.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -20,9 +19,80 @@ Or install it yourself as:
|
|
20
19
|
|
21
20
|
$ gem install gdatastore_mapper
|
22
21
|
|
22
|
+
## Configuration
|
23
|
+
|
24
|
+
GdatastoreMapper configuration can be done through a database.yml. The simplest configuration is as follows, which sets the emulator_host to "localhost:8444" and dataset_id.
|
25
|
+
|
26
|
+
```
|
27
|
+
# config/database.yml
|
28
|
+
production:
|
29
|
+
dataset_id: your-google-cloud-platform-project-id
|
30
|
+
|
31
|
+
staging:
|
32
|
+
dataset_id: your-google-cloud-platform-project-id
|
33
|
+
|
34
|
+
development:
|
35
|
+
dataset_id: your-google-cloud-platform-project-id
|
36
|
+
emulator_host: localhost:8444
|
37
|
+
|
38
|
+
test:
|
39
|
+
dataset_id: your-google-cloud-platform-project-id
|
40
|
+
emulator_host: localhost:8444
|
41
|
+
```
|
42
|
+
|
23
43
|
## Usage
|
24
44
|
|
25
|
-
|
45
|
+
Only 2 things you need to do.
|
46
|
+
|
47
|
+
1 To include GdatastoreMapper
|
48
|
+
2 To set attr_accessor as column
|
49
|
+
|
50
|
+
That's it!
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Book
|
54
|
+
include GdatastoreMapper::Base
|
55
|
+
|
56
|
+
attr_accessor :title, :author
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Persistence Methods
|
61
|
+
|
62
|
+
```
|
63
|
+
book = Book.new
|
64
|
+
book.title = 'Harry Potter'
|
65
|
+
book.save
|
66
|
+
```
|
67
|
+
```
|
68
|
+
book = Book.new(title: 'Harry Potter')
|
69
|
+
book.save
|
70
|
+
```
|
71
|
+
```
|
72
|
+
Book.create(title: 'Harry Potter'
|
73
|
+
```
|
74
|
+
```
|
75
|
+
book.update(title: 'Harry Potter 2'
|
76
|
+
```
|
77
|
+
```
|
78
|
+
book.delete
|
79
|
+
```
|
80
|
+
|
81
|
+
## Scoping Methods
|
82
|
+
|
83
|
+
```
|
84
|
+
Book.where(title: 'Harry Potter')
|
85
|
+
```
|
86
|
+
```
|
87
|
+
Book.find(12)
|
88
|
+
```
|
89
|
+
```
|
90
|
+
Book.find_by(title: 'Harry Potter')
|
91
|
+
```
|
92
|
+
```
|
93
|
+
Book.order(title: :asc)
|
94
|
+
```
|
95
|
+
|
26
96
|
|
27
97
|
## Development
|
28
98
|
|
data/gdatastore_mapper.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{Google Cloud Datastore Mapper in Ruby / Ruby on Rails}
|
13
13
|
spec.description = %q{Google Cloud Datastore ORM/ODM (Mapper) in Ruby and Ruby on Rails}
|
14
|
-
spec.homepage = "https://github.com/shinyaK14/
|
14
|
+
spec.homepage = "https://github.com/shinyaK14/gdatastore_mapper"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "google/cloud"
|
2
|
+
|
3
|
+
module GdatastoreMapper
|
4
|
+
module Associations
|
5
|
+
|
6
|
+
def has_many models
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def belongs_to model
|
11
|
+
# if @belongs_to_mode.defined?
|
12
|
+
@belongs = model
|
13
|
+
|
14
|
+
define_method @belongs do
|
15
|
+
p 'succseed'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "google/cloud/datastore"
|
2
|
+
require "gdatastore_mapper/session"
|
3
|
+
require "gdatastore_mapper/scoping"
|
4
|
+
require "gdatastore_mapper/associations"
|
5
|
+
require "gdatastore_mapper/persistence"
|
6
|
+
|
7
|
+
module GdatastoreMapper
|
8
|
+
module Base
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
include ActiveModel::Model
|
11
|
+
include ActiveModel::Validations
|
12
|
+
|
13
|
+
attr_accessor :id, :created_at, :updated_at
|
14
|
+
|
15
|
+
def self.included(klass)
|
16
|
+
klass.extend Scoping
|
17
|
+
klass.extend Associations
|
18
|
+
klass.include Persistence
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
|
23
|
+
def attr_accessor(*vars)
|
24
|
+
@attributes ||= []
|
25
|
+
@attributes.concat vars
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def attributes
|
30
|
+
@attributes.reject do |attr|
|
31
|
+
[:id, :created_at, :updated_at].include? attr
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def from_entity entity
|
36
|
+
record = self.new
|
37
|
+
record.id = entity.key.id
|
38
|
+
entity.properties.to_hash.each do |name, value|
|
39
|
+
record.send "#{name}=", value if record.respond_to? "#{name}="
|
40
|
+
end
|
41
|
+
record
|
42
|
+
end
|
43
|
+
|
44
|
+
def create params
|
45
|
+
record = self.new params
|
46
|
+
record.save
|
47
|
+
record
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
def attributes
|
53
|
+
self.class.attributes
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_entity
|
57
|
+
entity = Google::Cloud::Datastore::Entity.new
|
58
|
+
entity.key = Google::Cloud::Datastore::Key.new self.class.to_s, id
|
59
|
+
|
60
|
+
attributes.each do |attribute|
|
61
|
+
entity[attribute] = self.send attribute if self.send attribute
|
62
|
+
end
|
63
|
+
|
64
|
+
entity['created_at'] = id ? self.created_at : Time.zone.now
|
65
|
+
entity["updated_at"] = Time.zone.now
|
66
|
+
entity
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "google/cloud"
|
2
|
+
|
3
|
+
module GdatastoreMapper
|
4
|
+
module Persistence
|
5
|
+
|
6
|
+
def save
|
7
|
+
return false if !valid?
|
8
|
+
entity = to_entity
|
9
|
+
GdatastoreMapper::Session.dataset.save(entity)
|
10
|
+
self.id = entity.key.id
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def update attributes
|
15
|
+
attributes.each do |name, value|
|
16
|
+
send "#{name}=", value if respond_to? "#{name}="
|
17
|
+
end
|
18
|
+
save
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
GdatastoreMapper::Session.dataset.delete \
|
23
|
+
Google::Cloud::Datastore::Key.new self.class.to_s, id
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete
|
27
|
+
GdatastoreMapper::Session.dataset.delete \
|
28
|
+
Google::Cloud::Datastore::Key.new self.class.to_s, id
|
29
|
+
end
|
30
|
+
|
31
|
+
def persisted?
|
32
|
+
id.present?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "google/cloud"
|
2
|
+
|
3
|
+
module GdatastoreMapper
|
4
|
+
module Scoping
|
5
|
+
|
6
|
+
def where condition
|
7
|
+
return nil unless condition.is_a?(Hash)
|
8
|
+
entities = dataset_run(where_query condition)
|
9
|
+
end
|
10
|
+
|
11
|
+
def find id
|
12
|
+
return nil unless id.is_a?(Fixnum)
|
13
|
+
query = Google::Cloud::Datastore::Key.new self.to_s, id.to_i
|
14
|
+
entities = GdatastoreMapper::Session.dataset.lookup query
|
15
|
+
from_entity entities.first if entities.any?
|
16
|
+
end
|
17
|
+
|
18
|
+
def find_by condition
|
19
|
+
return nil unless condition.is_a?(Hash)
|
20
|
+
where(condition)&.first
|
21
|
+
end
|
22
|
+
|
23
|
+
def order condition
|
24
|
+
return nil unless condition.is_a?(Hash)
|
25
|
+
dataset_run(order_query condition)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def where_query condition
|
31
|
+
query = Google::Cloud::Datastore::Query.new
|
32
|
+
query.kind self.to_s
|
33
|
+
condition.each do |property, value|
|
34
|
+
query.where(property.to_s, '=', value)
|
35
|
+
end
|
36
|
+
query
|
37
|
+
end
|
38
|
+
|
39
|
+
def order_query condition
|
40
|
+
query = Google::Cloud::Datastore::Query.new
|
41
|
+
query.kind self.to_s
|
42
|
+
condition.each do |property, value|
|
43
|
+
query.order(property.to_s, value)
|
44
|
+
end
|
45
|
+
query
|
46
|
+
end
|
47
|
+
|
48
|
+
def dataset_run query
|
49
|
+
entities = GdatastoreMapper::Session.dataset.run query
|
50
|
+
entities.map do |entity|
|
51
|
+
from_entity entity
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "google/cloud"
|
2
|
+
|
3
|
+
module GdatastoreMapper
|
4
|
+
class Session
|
5
|
+
|
6
|
+
def self.dataset
|
7
|
+
config = Rails.application.config.database_configuration[Rails.env]
|
8
|
+
@dataset ||= Google::Cloud::Datastore.new(
|
9
|
+
project: config['dataset_id'],
|
10
|
+
emulator_host: config['emulator_host']
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.destroy
|
15
|
+
@dataset = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/gdatastore_mapper.rb
CHANGED
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.1
|
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-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,8 +85,14 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- gdatastore_mapper.gemspec
|
87
87
|
- lib/gdatastore_mapper.rb
|
88
|
+
- lib/gdatastore_mapper/associations.rb
|
89
|
+
- lib/gdatastore_mapper/base.rb
|
90
|
+
- lib/gdatastore_mapper/persistence.rb
|
91
|
+
- lib/gdatastore_mapper/relation.rb
|
92
|
+
- lib/gdatastore_mapper/scoping.rb
|
93
|
+
- lib/gdatastore_mapper/session.rb
|
88
94
|
- lib/gdatastore_mapper/version.rb
|
89
|
-
homepage: https://github.com/shinyaK14/
|
95
|
+
homepage: https://github.com/shinyaK14/gdatastore_mapper
|
90
96
|
licenses:
|
91
97
|
- MIT
|
92
98
|
metadata: {}
|