iseshima_store 0.1.0 → 0.1.1

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: 438924eba446e1d4dc32b6aafa50e06b72d9f8fe
4
- data.tar.gz: 951f35188f1b536c4adf93508d23c5a25a86d0ab
3
+ metadata.gz: ad2a0c29f44f30df8ffc1b1bf676a9370731e13f
4
+ data.tar.gz: afa1dfa84d0ed4c3c6e8a3e2f816b2bc58ce971f
5
5
  SHA512:
6
- metadata.gz: 22e6b0c06186a3c1dd5f94b3a553c75305a944b587c28787b137299837ff6eed2f3fa1415028a2c4e423894dc90006400e8308a5128212c6fa454f729ccf1f60
7
- data.tar.gz: fa1824a8d2d212b156f6d77baa579c13ba587faa71d11e553c515e6b258a69253b44e513efb9cab22c0333efbe678f4e6523a298ee478823fb7576d9a21a178d
6
+ metadata.gz: ddfe993d85bb0aca3dc33d3eececd4bff23a710b670bee4a1c4e7550deef7cc726bea1395de39302cd0a31827ddefc6c364f552aa987d891434a4581e386788b
7
+ data.tar.gz: f84ce3880a1891d91fe213d72b38d7164849c488bc593eacf340e124c79b4278820287c8bb75f8fab21a22ec343b7b9c10d0f91ed853b34d2e93bbd6068aa4af
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # IseshimaStore
1
+ # IseshimaStore :dolls:
2
2
 
3
3
  Simple ruby ORM for Google Cloud Datastore.
4
4
 
@@ -20,6 +20,8 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ ### Setup
24
+
23
25
  You need to set project_id.
24
26
 
25
27
  ```
@@ -28,17 +30,53 @@ IseshimaStore::Connection.configure do |config|
28
30
  end
29
31
  ```
30
32
 
31
- Iseshima Store depends on `gcloud-ruby` which uses ENV variables.
33
+ Iseshima Store depends on `gcloud-ruby` which uses ENV variables, you can set your variables through ENV.
34
+
35
+ ### Model
36
+
37
+ ```
38
+ class User
39
+ include IseshimaStore::Base
40
+ attr_accessor :name, :email
41
+ end
42
+ ```
43
+
44
+ The only thing you have to do is just include `IseshimaStore::Base` in your model class.
45
+ Any class is ok to use.
46
+
47
+ ### Create
48
+
49
+ ```
50
+ user = User.new
51
+ user.email = 'test@test.com'
52
+ user.name = 'hoge@fuga.com'
53
+ user.save!
54
+ ```
55
+
56
+ IseshimaStore does not have validations.
57
+ Another gem like `ActiveModel` is recommended to combine.
32
58
 
33
- ## Development
59
+ ### Finder
34
60
 
35
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+ ```
62
+ users = User.where(email: 'test@test.com')
63
+ user = User.find_by(email: 'test@test.com')
64
+ user = User.find(12345) # id
65
+ ```
66
+
67
+ ### Low level search
68
+
69
+ ```
70
+ query = Gcloud::Datastore::Query.new
71
+ query.kind('User')
72
+ query.where('email', '=', 'test@test.com')
73
+ res = User.search(query: query)
74
+ users = res[:records]
75
+ ```
36
76
 
37
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+ If you need `limit` & `cursor`, use this API.
38
78
 
39
- ## Contributing
40
79
 
41
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/iseshima_store. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
42
80
 
43
81
 
44
82
  ## License
@@ -1,18 +1,49 @@
1
+ require 'forwardable'
1
2
  require 'gcloud/datastore'
3
+ require 'iseshima_store/relation'
4
+
5
+ #http://googlecloudplatform.github.io/gcloud-ruby/docs/master/Gcloud/Datastore.html
2
6
 
3
7
  module IseshimaStore
4
8
  module Base
9
+
5
10
  def self.included(klass)
11
+ klass.extend SingleForwardable
6
12
  klass.extend ClassMethods
13
+ klass.def_delegators :scoping, :where, :all, :to_a
14
+
15
+ klass.instance_eval do
16
+ attr_accessor :created_at, :description
17
+ end
7
18
  end
8
19
 
9
20
  module ClassMethods
10
21
  attr_reader :properties
11
22
 
23
+ def scoping
24
+ IseshimaStore::Relation.new(self)
25
+ end
26
+
27
+ def find(_id)
28
+ datastore = IseshimaStore::Connection.current
29
+ key = datastore.key(self.to_s, _id.to_i)
30
+ entity = datastore.find(key)
31
+ from_entity(entity)
32
+ end
33
+
12
34
  def attr_properties(*args)
13
35
  @properties = args
14
36
  end
15
37
 
38
+ def find_by(hash)
39
+ key, value = hash.keys.first.to_s, hash.values.first.to_s
40
+ query = Gcloud::Datastore::Query.new
41
+ query.kind(self.to_s)
42
+ query.where(key, '=', value)
43
+ results = IseshimaStore::Connection.current.run(query)
44
+ results.map { |entity| from_entity(entity) }.first
45
+ end
46
+
16
47
  def from_entity(entity)
17
48
  instance = self.new
18
49
  instance.id = entity.key.id
@@ -22,11 +53,17 @@ module IseshimaStore
22
53
  instance
23
54
  end
24
55
 
25
- def query(options = {})
26
- query = Gcloud::Datastore::Query.new
27
- query.kind(self.to_s)
28
- query.limit options[:limit] if options[:limit]
29
- query.cursor options[:cursor] if options[:cursor]
56
+ def search(options = {})
57
+ query =
58
+ if options[:query]
59
+ options[:query]
60
+ else
61
+ query = Gcloud::Datastore::Query.new
62
+ query.kind(self.to_s)
63
+ query.limit(options[:limit]) if options[:limit]
64
+ query.cursor(options[:cursor]) if options[:cursor]
65
+ query
66
+ end
30
67
 
31
68
  results = IseshimaStore::Connection.current.run(query)
32
69
  records = results.map { |entity| from_entity(entity) }
@@ -46,10 +83,16 @@ module IseshimaStore
46
83
  self
47
84
  end
48
85
 
86
+ def destroy
87
+ entity = to_entity
88
+ IseshimaStore::Connection.current.delete(entity)
89
+ end
90
+
49
91
  def to_entity
50
92
  entity = Gcloud::Datastore::Entity.new
51
93
  entity.key = Gcloud::Datastore::Key.new(self.class.to_s, id)
52
94
  self.class.properties.each do |property|
95
+ property = property.to_s
53
96
  entity[property] = send(property)
54
97
  end
55
98
  entity
@@ -0,0 +1,31 @@
1
+ module IseshimaStore
2
+ module QueryMethods
3
+ def where(condition)
4
+ spawn.where!(condition)
5
+ end
6
+
7
+ def where!(condition)
8
+ @where_clause += condition
9
+ self
10
+ end
11
+
12
+ def all
13
+ to_a
14
+ end
15
+
16
+ def to_a
17
+ query = Gcloud::Datastore::Query.new
18
+ query.kind(@klass.to_s)
19
+ @where_clause.conditions.each do |condition|
20
+ query.where(*condition)
21
+ end
22
+
23
+ results = IseshimaStore::Connection.current.run(query)
24
+ results.map { |entity| @klass.from_entity(entity) }
25
+ end
26
+
27
+ def inspect
28
+ to_a.inspect
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require 'iseshima_store/query_methods'
2
+ require 'iseshima_store/where_clause'
3
+
4
+ module IseshimaStore
5
+ class Relation
6
+ include Enumerable
7
+ include IseshimaStore::QueryMethods
8
+ attr_accessor :where_clause
9
+
10
+ def initialize(klass)
11
+ @klass = klass
12
+ @where_clause = IseshimaStore::WhereClause.new
13
+ end
14
+
15
+ def spawn
16
+ clone
17
+ end
18
+
19
+ def each
20
+ to_a.each { |obj| yield obj }
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module IseshimaStore
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,27 @@
1
+ module IseshimaStore
2
+ class WhereClause
3
+ attr_reader :conditions
4
+
5
+ def initialize
6
+ @conditions = []
7
+ end
8
+
9
+ def +(condition)
10
+
11
+ # conditions like where(name: 'taro', email: 'taro@gmail.com')
12
+ if condition.is_a?(Hash)
13
+ condition.each do |key, value|
14
+ @conditions << [key.to_s, '=', value]
15
+ end
16
+ # condisions like where('age', '>=', 16)
17
+ elsif condition.is_a?(Array) && condition.length == 3
18
+ @conditions << condition
19
+ # Other
20
+ else
21
+ raise ArgumentError.new("wrong format of arguments")
22
+ end
23
+
24
+ self
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iseshima_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kotohata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,7 +86,10 @@ files:
86
86
  - lib/iseshima_store.rb
87
87
  - lib/iseshima_store/base.rb
88
88
  - lib/iseshima_store/connection.rb
89
+ - lib/iseshima_store/query_methods.rb
90
+ - lib/iseshima_store/relation.rb
89
91
  - lib/iseshima_store/version.rb
92
+ - lib/iseshima_store/where_clause.rb
90
93
  homepage: https://github.com/attracie/iseshima_store
91
94
  licenses:
92
95
  - MIT