gdatastore_mapper 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21b840689c7c1696a1d03a4e94780db8d29f6e4b
4
- data.tar.gz: 0e55089540444e6f7d37dad011a6a52847d533c2
3
+ metadata.gz: e000cc4729803880f3e3e8ff0ec67db4e6569c1d
4
+ data.tar.gz: 567ca61e5a2f5e8aab79f10dd8fe370dd01678db
5
5
  SHA512:
6
- metadata.gz: ab14005088a2fae2c1219b18224517e141e0402bc266a4653818d20b5201feaee2930b889438603367ab666499b9a59274cb40523f8accae25b1610bbd50574c
7
- data.tar.gz: 1e2bcbdcd38a22b70eba990ef9571659ccf26aeac6ca135e7fc53cbc6cf24fa344b991e2bdc657d8235d36c545890f0ed31c672eac69419840355530184a8148
6
+ metadata.gz: e202e556bb85658c2527f4bc553c01162b5f630c3570677ff780193e4be27f2a30c1f3bb9ff969441957ac939727acbba3d208dd43642001521ea0d58b19ae2c
7
+ data.tar.gz: 753114eb2ecdec0bc736e164c0972973d6a771d34766eb8426e9557d5370040bc2ede8a5cea96fe4a8ad84aac7fa991756e33862a85c5d0a448ef80aa5086f7e
data/README.md CHANGED
@@ -15,6 +15,7 @@ Once you install GdatastoreMapper you can use Google Cloud Datastore like Active
15
15
  - [Timestamp](#timestamp)
16
16
  - [Associations](#associations)
17
17
  - [One to Many](#one-to-many)
18
+ - [Method Chaining](#method-chaining)
18
19
  - [Callbacks](#callbacks)
19
20
  - [Validations](#validations)
20
21
  - [Contact](#contact)
@@ -212,6 +213,16 @@ rowling.books.count
212
213
  harry_poter.author
213
214
  => [#<Author:0x00 @name="J. K. Rowling" .... ]
214
215
  ```
216
+ ### Method Chaining
217
+
218
+ ```
219
+ rowling.books.order(created_at: :desc).limit(3)
220
+ => [#<Book:0x00 @title="Harry Potter" .... ]
221
+ ```
222
+ ```
223
+ rowling.books.where(series: 'Harry Potter').order(published_at: :asc).limit(3)
224
+ => [#<Book:0x00 @title="Harry Potter" .... ]
225
+ ```
215
226
 
216
227
  ## Callbacks
217
228
 
@@ -1,5 +1,11 @@
1
+ require 'gdatastore_mapper/relation/query_methods'
2
+
1
3
  module GdatastoreMapper
2
4
  class Relation < Array
5
+ include QueryMethods
6
+
7
+ attr_accessor :klass, :association
8
+
3
9
  def initialize(klass, association)
4
10
  @klass = klass
5
11
  @association = association
@@ -30,5 +36,6 @@ module GdatastoreMapper
30
36
  owner_attr[@association.belonging_id] = (existing_ids << belonging.id)
31
37
  @association.owner.update(owner_attr)
32
38
  end
39
+
33
40
  end
34
41
  end
@@ -0,0 +1,83 @@
1
+ module GdatastoreMapper
2
+ module QueryMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ def order condition, &block
6
+ return nil unless condition.is_a?(Hash)
7
+ dataset_run(order_query(condition), &block)
8
+ end
9
+
10
+ def where condition, &block
11
+ return nil unless condition.is_a?(Hash)
12
+ dataset_run(where_query(condition), &block)
13
+ end
14
+
15
+ def find id
16
+ return nil if id.nil?
17
+ query = Google::Cloud::Datastore::Key.new in_class, id.to_i
18
+ entities = GdatastoreMapper::Session.dataset.lookup query
19
+ from_entity entities.first if entities.any?
20
+ end
21
+
22
+ def find_by condition
23
+ return nil unless condition.is_a?(Hash)
24
+ where(condition)&.first
25
+ end
26
+
27
+ def find_or_create condition
28
+ return nil unless condition.is_a?(Hash)
29
+ if record = where(condition)&.first
30
+ record
31
+ else
32
+ create condition
33
+ end
34
+ end
35
+
36
+ def limit condition
37
+ return nil unless condition.is_a?(Fixnum)
38
+ self[0..condition-1]
39
+ end
40
+
41
+ private
42
+
43
+ def in_class
44
+ self&.first&.class&.to_s
45
+ end
46
+
47
+ def order_query condition
48
+ query = Google::Cloud::Datastore::Query.new.kind(in_class)
49
+ condition.each do |property, value|
50
+ query.order(property.to_s, value)
51
+ end
52
+ query
53
+ end
54
+
55
+ def where_query condition
56
+ query = Google::Cloud::Datastore::Query.new.kind(in_class)
57
+ condition.each do |property, value|
58
+ query.where(property.to_s, '=', value)
59
+ end
60
+ query
61
+ end
62
+
63
+ def dataset_run query, &block
64
+ entities = GdatastoreMapper::Session.dataset.run query
65
+ result = GdatastoreMapper::Relation.new(in_class, self.association)
66
+ entities.each do |entity|
67
+ record = from_entity(entity)
68
+ block.call(record) if block_given?
69
+ result << record if record
70
+ end
71
+ result
72
+ end
73
+
74
+ def from_entity entity
75
+ record = self.first.class.new
76
+ record.id = entity.key.id
77
+ entity.properties.to_hash.each do |name, value|
78
+ record.send "#{name}=", value if record.respond_to? "#{name}="
79
+ end
80
+ record
81
+ end
82
+ end
83
+ end
@@ -51,11 +51,15 @@ module GdatastoreMapper
51
51
  all.count
52
52
  end
53
53
 
54
+ def limit condition
55
+ return nil unless condition.is_a?(Fixnum)
56
+ all[0..condition-1]
57
+ end
58
+
54
59
  private
55
60
 
56
61
  def where_query condition
57
- query = Google::Cloud::Datastore::Query.new
58
- query.kind self.to_s
62
+ query = Google::Cloud::Datastore::Query.new.kind(self.to_s)
59
63
  condition.each do |property, value|
60
64
  query.where(property.to_s, '=', value)
61
65
  end
@@ -63,8 +67,7 @@ module GdatastoreMapper
63
67
  end
64
68
 
65
69
  def order_query condition
66
- query = Google::Cloud::Datastore::Query.new
67
- query.kind self.to_s
70
+ query = Google::Cloud::Datastore::Query.new.kind(self.to_s)
68
71
  condition.each do |property, value|
69
72
  query.order(property.to_s, value)
70
73
  end
@@ -1,3 +1,3 @@
1
1
  module GdatastoreMapper
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  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.7
4
+ version: 0.1.8
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-24 00:00:00.000000000 Z
11
+ date: 2017-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-datastore
@@ -120,6 +120,7 @@ files:
120
120
  - lib/gdatastore_mapper/persistence.rb
121
121
  - lib/gdatastore_mapper/persistence/class_methods.rb
122
122
  - lib/gdatastore_mapper/relation.rb
123
+ - lib/gdatastore_mapper/relation/query_methods.rb
123
124
  - lib/gdatastore_mapper/scoping.rb
124
125
  - lib/gdatastore_mapper/session.rb
125
126
  - lib/gdatastore_mapper/validations.rb