Sha256: b320994115b0dc1ef64598d6dfe545ef7e0609204fceb38d691d58e4746cacf2

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

module Rico
  module Object
    extend Forwardable

    def_delegators :riak_object, :conflict?, :content_type, :content_type=, :data, :delete, :store, :raw_data

    attr_accessor :bucket, :key

    # Initialize an object with a bucket and key
    #
    # bucket - the name of the bucket (not prefixed by a namespace)
    # key - the name of the key
    #
    # Returns nothing
    def initialize(bucket, key, options={})
      @bucket, @key = bucket, key
      options.each {|k,v| send("#{k}=", v)}
    end

    # Sets a new value on the object and stores it
    #
    # value - new value to set
    #
    # Returns the result of the store operation
    def mutate(value)
      riak_object.data = value
      store
    end

    # Determine whether an object exists or not
    #
    # Returns true or false
    def exists?
      Rico.bucket(@bucket).exists? @key
    end

    protected

    def type_key
      name = self.class.name.split("::").last
      Rico::TYPES[name]
    end

    def riak_object
      @riak_object ||= Rico.bucket(@bucket).get_or_new @key
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rico-0.4.0 lib/rico/object.rb
rico-0.3.0 lib/rico/object.rb