Class: NCMB::DataStore

Inherits:
Object
  • 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



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



17
18
19
20
21
22
23
# File 'lib/ncmb/data_store.rb', line 17

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



74
75
76
77
# File 'lib/ncmb/data_store.rb', line 74

def [](count)
  return @@items[count] unless @@items.nil?
  get(@@queries)[count]
end

#call(name) ⇒ Object



25
26
27
# File 'lib/ncmb/data_store.rb', line 25

def call(name)
  @@fields[name.to_sym] || NoMethod
end

#columnsObject



13
14
15
# File 'lib/ncmb/data_store.rb', line 13

def columns
  @@fields.keys
end

#count(count) ⇒ Object



52
53
54
55
# File 'lib/ncmb/data_store.rb', line 52

def count(count)
  @@queries[:count] = count
  self
end

#each(&block) ⇒ Object



29
30
31
# File 'lib/ncmb/data_store.rb', line 29

def each(&block)
  @@items.each(&block)
end

#each_with_index(&block) ⇒ Object



33
34
35
# File 'lib/ncmb/data_store.rb', line 33

def each_with_index(&block)
  @@items.each_with_index(&block)
end

#firstObject



42
43
44
45
# File 'lib/ncmb/data_store.rb', line 42

def first
  return @@items.first unless @@items.nil?
  get(@@queries).first
end

#get(queries = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ncmb/data_store.rb', line 79

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



47
48
49
50
# File 'lib/ncmb/data_store.rb', line 47

def limit(count)
  @@queries[:limit] = count
  self
end

#order(field) ⇒ Object



37
38
39
40
# File 'lib/ncmb/data_store.rb', line 37

def order(field)
  @@queries[:order] = field
  self
end

#post(queries = {}) ⇒ Object



96
97
98
99
100
# File 'lib/ncmb/data_store.rb', line 96

def post(queries = {})
  path = "/#{@client.api_version}/classes/#{@@name}"
  result = @client.post path, queries
  NCMB::DataStore.new(client, name, result)
end

#skip(count) ⇒ Object



57
58
59
60
# File 'lib/ncmb/data_store.rb', line 57

def skip(count)
  @@queries[:skip] = count
  self
end

#where(params = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ncmb/data_store.rb', line 62

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