Sha256: faf46345fd610b7d38bf964fe321f9447cfd9000c83662390960b60832a5ffa9

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module NCMB
  class Object
    include NCMB

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

    def set(name, value)
      @fields[name.to_sym] = value
    end
    
    def call(name)
      @fields[name.to_sym] || NoMethodError
    end
    
    def [](key)
      @fields[key]
    end

    def post
      path = "/#{@@client.api_version}/classes/#{@name}"
      result = @@client.post path, @fields
      @fields.merge!(result)
      self
    end
    alias :save :post
    
    def put
      path = "/#{@@client.api_version}/classes/#{@name}/#{@fields[:objectId]}"
      params = @fields
      params.delete :objectId
      params.delete :createDate
      params.delete :updateDate
      result = @@client.put path, params
      @fields[:updateDate] = result[:updateDate]
      self
    end
    alias :update :put
    
    def delete
      path = "/#{@@client.api_version}/classes/#{@name}/#{@fields[:objectId]}"
      response = @@client.delete path, {}
      if response == true
        return true
      else
        @@last_error = response
        return false
      end
    end
    
    def error
      @@last_error
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ncmb-ruby-client-0.0.9 lib/ncmb/object.rb