Module: RiakRest::JiakResourcePOV::ClassMethods

Defined in:
lib/riakrest/resource/jiak_resource_pov.rb

Overview

Class methods for creating a user-defined JiakResourcePOV.

See JiakResourcePOV for example usage.

Instance Method Summary collapse

Instance Method Details

#attr_accessor(*fields) ⇒ Object

:call-seq:

attr_accessor :f1,...,:fn

Add read/write accessible fields.



98
99
100
101
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 98

def attr_accessor(*fields)
  attr_reader(*fields)
  attr_writer(*fields)
end

#attr_reader(*fields) ⇒ Object

:call-seq:

attr_reader :f1,...,:fn

Add read accessible fields.



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 62

def attr_reader(*fields)
  check_fields(fields,@resource.schema.read_mask)
  added_fields = @jiak.bucket.data_class.readable(*fields)
  added_fields.each do |field|
    class_eval <<-EOM
      def #{field}
        @jiak.object.data.#{field}
      end
    EOM
  end
  @jiak.read_mask = @jiak.bucket.data_class.schema.read_mask.join(',')
  nil
end

#attr_writer(*fields) ⇒ Object

:call-seq:

attr_writer :f1,...,:fn

Add write accessible fields.



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 80

def attr_writer(*fields)
  check_fields(fields,@resource.schema.write_mask)
  added_fields = @jiak.bucket.data_class.writable(*fields)
  added_fields.each do |field|
    class_eval <<-EOM
      def #{field}=(val)
        @jiak.object.data.#{field} = val
        self.class.do_auto_update(self)
      end
    EOM
  end
  nil
end

#do_auto_update(rsrc) ⇒ Object

:call-seq:

JiakResourcePOV.do_auto_update(resource)  -> JiakResourcePOV or nil

Determine if an auto update should be done on the resource and perform an update if so.

Public method as a by-product of implementation.



174
175
176
177
178
179
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 174

def do_auto_update(rsrc)  # :no-doc:
  if(rsrc.auto_update? ||
      ((rsrc.auto_update? != false) && rsrc.class.auto_update?))
    update(rsrc)
  end
end

#exist?(key) ⇒ Boolean

:call-seq:

JiakResourcePOV.exist?(key) -> true or false

Determine if a resource exists on the Jiak server for a key.

Returns:

  • (Boolean)


163
164
165
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 163

def exist?(key)
  @resource.jiak.client.exist?(@jiak.bucket,key)
end

#get(key, opts = {}) ⇒ Object

:call-seq:

JiakResourcePOV.get(key,opts={})  -> JiakResourcePOV

Get a JiakResourcePOV on the Jiak server by the specified key.

Valid options:

:reads — See JiakResource#get

Raise JiakResourceNotFound if no resource exists on the Jiak server for the key.



131
132
133
134
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 131

def get(key,opts={})
  opts[:read] = @jiak.read_mask
  new(@resource.jiak.client.get(@jiak.bucket,key,opts))
end

#keysObject

:call-seq:

JiakResourcePOV.keys   -> array

Get an array of the current keys for this resource. Since key lists are updated asynchronously on a Riak cluster the returned array can be out of synch immediately after new puts or deletes.



117
118
119
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 117

def keys
  @resource.jiak.client.keys(jiak.bucket)
end

#refresh(resource, opts = {}) ⇒ Object

:call-seq:

JiakResourcePOV.refresh(resource,opts={})  -> JiakResourcePOV

Updates a JiakResource with the data on the Jiak server. The current data of the JiakResource is overwritten, so use with caution.

See JiakResource#refresh for options.



155
156
157
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 155

def refresh(resource,opts={})
  resource.jiak.object = get(resource.jiak.object.key,opts).jiak.object
end

#resource(resource) ⇒ Object

:call-seq:

JiakResourcePOV.resource(resource)

Set the JiakResource to which this JiakResourcePOV is a point-of-view.



52
53
54
55
56
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 52

def resource(resource)
  @resource = resource
  @jiak.bucket = JiakBucket.new(@resource.jiak.group,
                                JiakDataFields.create)
end

#update(resource, opts = {}) ⇒ Object

:call-seq:

JiakResourcePOV.update(JiakResourcePOV,opts={})  -> JiakResourcePOV

Updates a JiakResourcePOV on the Jiak server.

See JiakResource#put for options.



142
143
144
145
146
# File 'lib/riakrest/resource/jiak_resource_pov.rb', line 142

def update(resource,opts={})
  opts[:copy] = true
  opts[:read] = @jiak.read_mask
  @resource.put(resource,opts)
end