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
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 = {}
@@items = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/ncmb/data_store.rb', line 21
def method_missing(name)
if @@fields[name.to_sym]
return @@fields[name.to_sym]
else
raise NoMethod, "#{name} is not found"
end
end
|
Instance Method Details
#[](count) ⇒ Object
78
79
80
81
|
# File 'lib/ncmb/data_store.rb', line 78
def [](count)
return @@items[count] unless @@items.nil?
get(@@queries)[count]
end
|
#call(name) ⇒ Object
29
30
31
|
# File 'lib/ncmb/data_store.rb', line 29
def call(name)
@@fields[name.to_sym] || NoMethod
end
|
#columns ⇒ Object
17
18
19
|
# File 'lib/ncmb/data_store.rb', line 17
def columns
@@fields.keys
end
|
#count(count) ⇒ Object
56
57
58
59
|
# File 'lib/ncmb/data_store.rb', line 56
def count(count)
@@queries[:count] = count
self
end
|
#each(&block) ⇒ Object
33
34
35
|
# File 'lib/ncmb/data_store.rb', line 33
def each(&block)
@@items.each(&block)
end
|
#each_with_index(&block) ⇒ Object
37
38
39
|
# File 'lib/ncmb/data_store.rb', line 37
def each_with_index(&block)
@@items.each_with_index(&block)
end
|
#first ⇒ Object
46
47
48
49
|
# File 'lib/ncmb/data_store.rb', line 46
def first
return @@items.first unless @@items.nil?
get(@@queries).first
end
|
#get(queries = {}) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/ncmb/data_store.rb', line 83
def get(queries = {})
path = "/#{@@client.api_version}/classes/#{@@name}"
results = @@client.get path, queries
return [] unless results
if results[:error] && results[:error] != ""
@@error = results
raise 'error'
end
items = []
results[:results].each do |result|
alc = result[:acl]
result.delete(:acl)
items << NCMB::DataStore.new(@@name, result, alc)
end
@@items = items
end
|
#limit(count) ⇒ Object
51
52
53
54
|
# File 'lib/ncmb/data_store.rb', line 51
def limit(count)
@@queries[:limit] = count
self
end
|
#new(*opt) ⇒ Object
13
14
15
|
# File 'lib/ncmb/data_store.rb', line 13
def new *opt
initialize 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
|
#post(queries = {}) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/ncmb/data_store.rb', line 100
def post(queries = {})
path = "/#{@@client.api_version}/classes/#{@@name}"
result = @@client.post path, queries
alc = result[:acl]
result.delete(:acl)
NCMB::DataStore.new(@@name, result, alc)
end
|
#skip(count) ⇒ Object
61
62
63
64
|
# File 'lib/ncmb/data_store.rb', line 61
def skip(count)
@@queries[:skip] = count
self
end
|
#where(params = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/ncmb/data_store.rb', line 66
def where(params = {})
@@queries[:where] = [] unless @@queries[:where]
if params.size == 1
@@queries[:where] << params
else
params.each do |hash|
@@queries[:where] << hash
end
end
self
end
|