Class: NCMB::DataStore
- Inherits:
-
Object
show all
- Includes:
- NCMB
- Defined in:
- lib/ncmb/data_store.rb
Constant Summary
Constants included
from NCMB
API_VERSION, DOMAIN
Instance Method Summary
collapse
Methods included from NCMB
CurrentUser, initialize
Constructor Details
#initialize(name, fields = {}, alc = "") ⇒ DataStore
Returns a new instance of DataStore.
5
6
7
8
9
10
11
|
# File 'lib/ncmb/data_store.rb', line 5
def initialize(name, fields = {}, alc = "")
@name = name
@alc = alc
@fields = fields
@queries = {where: []}
@items = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/ncmb/data_store.rb', line 25
def method_missing(name)
if @fields[name.to_sym]
return @fields[name.to_sym]
else
raise NoMethodError, "#{name} is not found"
end
end
|
Instance Method Details
#[](count) ⇒ Object
130
131
132
|
# File 'lib/ncmb/data_store.rb', line 130
def [](count)
get[count]
end
|
21
22
23
|
# File 'lib/ncmb/data_store.rb', line 21
def columns
@fields.keys
end
|
55
56
57
58
|
# File 'lib/ncmb/data_store.rb', line 55
def count
@queries[:count] = 1
self
end
|
#delete_all ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/ncmb/data_store.rb', line 168
def delete_all
max = 1000
dataStore = NCMB::DataStore.new(@name)
count = dataStore.limit(max).count().all
if count == 0
return true
end
dataStore.queries.delete :count
dataStore.each do |item|
item.delete
end
if count > max
return delete_all
end
true
end
|
#each(&block) ⇒ Object
33
34
35
|
# File 'lib/ncmb/data_store.rb', line 33
def each(&block)
get.each(&block)
end
|
#each_with_index(&block) ⇒ Object
37
38
39
|
# File 'lib/ncmb/data_store.rb', line 37
def each_with_index(&block)
get.each_with_index(&block)
end
|
13
14
15
|
# File 'lib/ncmb/data_store.rb', line 13
def error
@error
end
|
46
47
48
|
# File 'lib/ncmb/data_store.rb', line 46
def first
get.first
end
|
#get ⇒ Object
Also known as:
all
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/ncmb/data_store.rb', line 134
def get
return @items unless @items.nil?
path = "/#{@@client.api_version}/classes/#{@name}"
results = @@client.get path, @queries
return [] unless results
if results[:error] && results[:error] != ""
@error = results
raise NCMB::FetchError
end
if @queries[:count] == 1
return results[:count]
end
@items = []
results[:results].each do |result|
result.each do |key, field|
if field.is_a?(Hash) && field[:__type] == 'GeoPoint'
result[key] = NCMB::GeoPoint.new(field[:latitude], field[:longitude])
end
if field.is_a?(Hash) && field[:__type] == 'Date'
result[key] = Time.parse(field[:iso])
end
end
alc = result[:acl]
result.delete(:acl)
@items << NCMB::Object.new(@name, result, alc)
end
@items
end
|
#limit(count) ⇒ Object
50
51
52
53
|
# File 'lib/ncmb/data_store.rb', line 50
def limit(count)
@queries[:limit] = count
self
end
|
#new(opt = {}) ⇒ Object
17
18
19
|
# File 'lib/ncmb/data_store.rb', line 17
def new opt = {}
NCMB::Object.new @name, opt
end
|
#order(field) ⇒ Object
41
42
43
44
|
# File 'lib/ncmb/data_store.rb', line 41
def order(field)
@queries[:order] = field
self
end
|
164
165
166
|
# File 'lib/ncmb/data_store.rb', line 164
def queries
@queries
end
|
#skip(count) ⇒ Object
60
61
62
63
|
# File 'lib/ncmb/data_store.rb', line 60
def skip(count)
@queries[:skip] = count
self
end
|
#where(name, value) ⇒ Object
123
124
125
126
127
128
|
# File 'lib/ncmb/data_store.rb', line 123
def where(name, value)
params = {}
params[name] = value
@queries[:where] << params
self
end
|
#withinSquare(name, geo1, geo2) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/ncmb/data_store.rb', line 109
def withinSquare(name, geo1, geo2)
params = {}
params[name] = {
"$within": {
"$box": [
geo1,
geo2
]
}
}
@queries[:where] << params
self
end
|