Class: NCMB::DataStore

Inherits:
Object
  • Object
show all
Includes:
NCMB, Query
Defined in:
lib/ncmb/data_store.rb

Direct Known Subclasses

Devise

Constant Summary

Constants included from NCMB

API_VERSION, DOMAIN, SCRIPT_API_VERSION, SCRIPT_DOMAIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Query

#where, #withinSquare

Methods included from NCMB

CurrentUser, initialize

Constructor Details

#initialize(name, fields = {}, alc = "") ⇒ DataStore

Returns a new instance of DataStore.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ncmb/data_store.rb', line 9

def initialize(name, fields = {}, alc = "")
  @name    = name
  @alc     = alc
  @fields  = fields
  @search_key = :where
  @queries = {}
  @queries[@search_key] = []
  @items   = nil
  @path    = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ncmb/data_store.rb', line 33

def method_missing(name)
  if @fields[name.to_sym]
    return @fields[name.to_sym]
  else
    raise NoMethodError, "#{name} is not found"
  end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



19
20
21
# File 'lib/ncmb/data_store.rb', line 19

def path
  @path
end

Instance Method Details

#[](count) ⇒ Object



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

def [](count)
  get[count]
end

#columnsObject



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

def columns
  @fields.keys
end

#countObject



63
64
65
66
# File 'lib/ncmb/data_store.rb', line 63

def count
  @queries[:count] = 1
  self
end

#delete_allObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ncmb/data_store.rb', line 121

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|
    begin
      item.delete if item.deletable?
    rescue
      puts "Can't delete #{item.objectId}"
    end
  end
  if count > max
    return delete_all
  end
  true
end

#each(&block) ⇒ Object



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

def each(&block)
  get.each(&block)
end

#each_with_index(&block) ⇒ Object



45
46
47
# File 'lib/ncmb/data_store.rb', line 45

def each_with_index(&block)
  get.each_with_index(&block)
end

#errorObject



21
22
23
# File 'lib/ncmb/data_store.rb', line 21

def error
  @error
end

#firstObject



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

def first
  get.first
end

#getObject Also known as: all



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ncmb/data_store.rb', line 90

def get
  # return @items unless @items.nil?
  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
    @items << NCMB::Object.new(@name, result)
  end
  @items
end

#limit(count) ⇒ Object



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

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

#new(opt = {}) ⇒ Object



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

def new opt = {}
  NCMB::Object.new @name, opt
end

#order(field) ⇒ Object



49
50
51
52
# File 'lib/ncmb/data_store.rb', line 49

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

#queriesObject



117
118
119
# File 'lib/ncmb/data_store.rb', line 117

def queries
  @queries
end

#skip(count) ⇒ Object



68
69
70
71
# File 'lib/ncmb/data_store.rb', line 68

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