Sha256: 4b273a2b78e6d14b9ecfdb2426bc0ba470f4339eb3a65a33922a7604cceb29e0

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

module NCMB
  class DataStore
    include NCMB
    
    def initialize(name, fields = {}, alc = "")
      @@name    = name
      @@alc     = alc
      @@fields  = fields
      @@queries = {}
      @@items   = nil
    end
    
    def method_missing(name)
      return @@fields[name.to_sym] if @@fields[name.to_sym]
    end
    
    def order(field)
      @@queries[:order] = field
      self
    end
    
    def limit(count)
      @@queries[:limit] = count
      self
    end
    
    def count(count)
      @@queries[:count] = count
      self
    end
    
    def skip(count)
      @@queries[:skip] = count
      self
    end
    
    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
    
    def [](count)
      return @@items[count] unless @@items.nil?
      get(@@queries)[count]
    end
    
    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
    
    def post(queries = {})
      path = "/#{@client.api_version}/classes/#{@@name}"
      result = @client.post path, queries
      NCMB::DataStore.new(client, name, result)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ncmb-ruby-client-0.0.3 lib/ncmb/data_store.rb
ncmb-ruby-client-0.0.2 lib/ncmb/data_store.rb