Sha256: 101b0e7f5630ff4e675f9fef4bf8c1f681f9490150d03acaf7413109d63bca7f

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

module Zoop
  class Model < ZoopObject

    def create
      update_model Zoop::Request.post(@custom_url || self.class.url, params: to_hash).run
      self
    end

    def update(params={})
      update_model Zoop::Request.put(@custom_url || url, params: params).run
      self
    end

    def save
      update_model Zoop::Request.put(@custom_url || url, params: unsaved_attributes).run
      self
    end

    def destroy
      update_model Zoop::Request.delete(url).run
      self
    end

    def url(*params)
      raise RequestError.new('Invalid ID') unless id.present?
      self.class.url CGI.escape(id.to_s), *params
    end

    class << self

      def create(*args, &block)
        self.new(*args, &block).create
      end

      def find_by_id(id)
        raise RequestError.new('Invalid ID') unless id.present?
        Zoop::Request.get(url id).call
      end
      alias :find :find_by_id

      def url(*params)
        ["/#{ CGI.escape underscored_class_name }s", *params].join '/'
      end

      def class_name
        self.name.split('::').last
      end

      def underscored_class_name
        class_name.gsub(/[a-z0-9][A-Z]/){|s| "#{s[0]}_#{s[1]}"}.downcase
      end
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
zoop-0.3.5 lib/zoop/model.rb
zoop-0.3.4 lib/zoop/model.rb
zoop-0.3.3 lib/zoop/model.rb
zoop-0.3.2 lib/zoop/model.rb
zoop-0.3.1 lib/zoop/model.rb
zoop-0.3.0 lib/zoop/model.rb
zoop-0.2.1 lib/zoop/model.rb
zoop-0.1.0 lib/zoop/model.rb