iseshima_store 0.1.2 → 0.1.3

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: 150b59a929f9e4d2d3c94fa364c133ca92ac1c35
4
- data.tar.gz: 06f6f06c95893c6936d873e459add5e3edeb8071
3
+ metadata.gz: e677b4e94938ba2ebb108ddff93bd1165fcaf852
4
+ data.tar.gz: 6189431bd15b92d340c4722a79b9ecb2b21fac3f
5
5
  SHA512:
6
- metadata.gz: c2ea30f42d2a7aacc96de5e1cc0f04ba43228a4de0e5ae1a3460816d7747a06af61165afb6da8142c8ee1cadf2d2903b19eff65ff39a2372ac5608454273bb27
7
- data.tar.gz: 3bd638ecd194ae779ce9529794055f4edf654a77f04e875934359868a9bae5483e7753a6cc3fc669ac393264c904505a4df794d4afb3deaae283106be3c86ac4
6
+ metadata.gz: 52fdbaddd2045fe46114d6b64815bc2e295d253e030ba33b23dd2adeb5ec5b274094bb63f6ff7f04bcec769db84f888b26a1ec584dc8f939adb90e3f77d22c3b
7
+ data.tar.gz: a8873566d8f87743350e9e5c8569f5918113c09c534f35c4cf68491a0d6dffe530a09a772d75472f7e16303ba42b27d4938166c539afac28834be27424d9fa00
@@ -11,29 +11,30 @@ module IseshimaStore
11
11
  def self.included(klass)
12
12
  klass.extend SingleForwardable
13
13
  klass.extend ClassMethods
14
- klass.def_delegators :scoping, :where, :all, :first, :last, :to_a
14
+ klass.def_delegators :scoping,
15
+ :where,
16
+ :all,
17
+ :first,
18
+ :last,
19
+ :to_a,
20
+ :parent,
21
+ :find,
22
+ :find_by
15
23
 
16
24
  klass.instance_eval do
17
- attr_accessor :id, :created_at, :description
25
+ attr_accessor :id, :created_at, :description, :parent_key
18
26
  end
19
27
  end
20
28
 
21
29
  module ClassMethods
22
30
  attr_reader :properties
23
31
 
24
- def scoping
25
- IseshimaStore::Relation.new(self)
32
+ def destroy_all
33
+ scoping.each(&:destroy)
26
34
  end
27
35
 
28
- def find(_id)
29
- datastore = IseshimaStore::Connection.current
30
- key = datastore.key(self.to_s, _id.to_i)
31
- entity = datastore.find(key)
32
- if entity
33
- from_entity(entity)
34
- else
35
- raise EntityNotFound.new("cannot find entity with id #{_id}")
36
- end
36
+ def scoping
37
+ IseshimaStore::Relation.new(self)
37
38
  end
38
39
 
39
40
  def attr_properties(*args)
@@ -41,16 +42,15 @@ module IseshimaStore
41
42
  @properties = args
42
43
  end
43
44
 
44
- def find_by(hash)
45
- where(hash).first
46
- end
47
-
48
45
  def from_entity(entity)
49
46
  instance = self.new
50
47
  instance.id = entity.key.id
51
48
  entity.properties.to_hash.each do |name, value|
52
- instance.send "#{name}=", value
49
+ if instance.respond_to?("#{name}=")
50
+ instance.send "#{name}=", value
51
+ end
53
52
  end
53
+ instance.parent_key = entity.key.parent
54
54
  instance
55
55
  end
56
56
 
@@ -79,21 +79,39 @@ module IseshimaStore
79
79
 
80
80
  def save!
81
81
  entity = to_entity
82
+ if @parent_key == ''
83
+ entity.key.parent = nil
84
+ else
85
+ entity.key.parent = @parent_key
86
+ end
82
87
  IseshimaStore::Connection.current.save(entity)
83
88
  self.id = entity.key.id
84
89
  self
85
90
  end
86
91
 
87
92
  def destroy
88
- entity = to_entity
89
- IseshimaStore::Connection.current.delete(entity)
93
+ if key.parent
94
+ key.parent = nil
95
+ end
96
+ IseshimaStore::Connection.current.delete(key)
97
+ end
98
+
99
+ def assign_entities(hash)
100
+ hash.each { |k, v|
101
+ k = k.to_s
102
+ if respond_to? "#{k}="
103
+ send("#{k}=", v)
104
+ end
105
+ }
106
+ self
90
107
  end
91
108
 
92
109
  def to_entity
93
110
  entity = Gcloud::Datastore::Entity.new
94
111
  entity.key = Gcloud::Datastore::Key.new(self.class.to_s, id)
95
112
  unless self.class.properties
96
- raise StandardError.new("You have to define attr_properties in your model")
113
+ logger = Logger.new(STDOUT)
114
+ logger.warn "You may have to define attr_properties in your model"
97
115
  end
98
116
 
99
117
  self.class.properties.each do |property|
@@ -106,5 +124,29 @@ module IseshimaStore
106
124
  end
107
125
  entity
108
126
  end
127
+
128
+ def parent=(model)
129
+ if model
130
+ key = model.to_entity.key
131
+ self.parent_key = key
132
+ else
133
+ self.parent_key = ''
134
+ end
135
+ end
136
+
137
+ def parent
138
+ return @parent if @parent
139
+
140
+ if @parent_key
141
+ klass = Object.const_get(@parent_key.kind)
142
+ if klass.include?(IseshimaStore::Base)
143
+ @parent = klass.find(@parent_key.id)
144
+ end
145
+ end
146
+ end
147
+
148
+ def key
149
+ to_entity.key
150
+ end
109
151
  end
110
152
  end
@@ -11,8 +11,16 @@ module IseshimaStore
11
11
  yield(config)
12
12
  end
13
13
 
14
+ def self.datastore
15
+ Gcloud.datastore(config.project_id)
16
+ end
17
+
14
18
  def self.current
15
19
  @current ||= Gcloud.datastore(config.project_id)
16
20
  end
21
+
22
+ def self.clear_connection!
23
+ @current = nil
24
+ end
17
25
  end
18
26
  end
@@ -9,17 +9,30 @@ module IseshimaStore
9
9
  self
10
10
  end
11
11
 
12
+ def parent(model)
13
+ if model.respond_to?(:key)
14
+ @parent_key = model.key
15
+ end
16
+ self
17
+ end
18
+
12
19
  def all
13
20
  to_a
14
21
  end
15
22
 
23
+ def exists?
24
+ to_a.length > 0
25
+ end
26
+
16
27
  def to_a
17
28
  query = Gcloud::Datastore::Query.new
18
29
  query.kind(@klass.to_s)
19
30
  @where_clause.conditions.each do |condition|
20
31
  query.where(*condition)
21
32
  end
22
-
33
+ if @parent_key
34
+ query.ancestor(@parent_key)
35
+ end
23
36
  results = IseshimaStore::Connection.current.run(query)
24
37
  results.map { |entity| @klass.from_entity(entity) }
25
38
  end
@@ -27,5 +40,17 @@ module IseshimaStore
27
40
  def inspect
28
41
  to_a.inspect
29
42
  end
43
+
44
+ def find(_id)
45
+ entity = where(id: _id).first
46
+ unless entity
47
+ raise EntityNotFound.new("cannot find entity with id #{_id}")
48
+ end
49
+ entity
50
+ end
51
+
52
+ def find_by(hash)
53
+ where(hash).first
54
+ end
30
55
  end
31
56
  end
@@ -12,7 +12,7 @@ module IseshimaStore
12
12
 
13
13
  def initialize(klass)
14
14
  @klass = klass
15
- @where_clause = IseshimaStore::WhereClause.new
15
+ @where_clause = IseshimaStore::WhereClause.new(@klass)
16
16
  end
17
17
 
18
18
  def spawn
@@ -1,3 +1,3 @@
1
1
  module IseshimaStore
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -2,7 +2,9 @@ module IseshimaStore
2
2
  class WhereClause
3
3
  attr_reader :conditions
4
4
 
5
- def initialize
5
+ def initialize(klass)
6
+ # whereチェーンされているモデルのクラス
7
+ @klass = klass
6
8
  @conditions = []
7
9
  end
8
10
 
@@ -10,8 +12,15 @@ module IseshimaStore
10
12
 
11
13
  # conditions like where(name: 'taro', email: 'taro@gmail.com')
12
14
  if condition.is_a?(Hash)
13
- condition.each do |key, value|
14
- @conditions << [key.to_s, '=', value]
15
+ condition.each do |property, value|
16
+ property = property.to_s
17
+ if property == 'id'
18
+ datastore = IseshimaStore::Connection.current
19
+ key = datastore.key(@klass.to_s, value.to_i)
20
+ @conditions << ['__key__', '=', key]
21
+ else
22
+ @conditions << [property, '=', value]
23
+ end
15
24
  end
16
25
  # condisions like where('age', '>=', 16)
17
26
  elsif condition.is_a?(Array) && condition.length == 3
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.2
4
+ version: 0.1.3
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-09 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler