Sha256: 90b8a373501e652d0002679541aa948a5e07a6f64f8839765b1053f28ee2764d

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

module MotionResource
  class Base
    def save(&block)
      @new_record ? create(&block) : update(&block)
    end
    
    def update(&block)
      self.class.put(member_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
        block.call json.blank? ? nil : self.class.instantiate(json) if block
      end
    end
  
    def create(&block)
      # weird heisenbug: Specs crash without that line :(
      dummy = self
      self.class.post(collection_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
        block.call json.blank? ? nil : self.class.instantiate(json) if block
      end
    end
  
    def destroy(&block)
      self.class.delete(member_url) do |response, json|
        block.call json.blank? ? nil : self.class.instantiate(json) if block
      end
    end
    
    def reload(&block)
      self.class.get(member_url) do |response, json|
        block.call json.blank? ? nil : self.class.instantiate(json) if block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion-resource-0.0.1 lib/motion-resource/crud.rb