Module: RiakRest::JiakData::ClassMethods

Defined in:
lib/riakrest/core/jiak_data.rb

Overview

Class methods for use in creating a user-defined JiakData. The methods allow, require, readable, writable delegate to JiakSchema. See JiakSchema for discussion on the use of schemas in Riak.

Instance Method Summary collapse

Instance Method Details

#allow(*fields) ⇒ Object

:call-seq:

allow :f1, ..., :fn   -> array
allow [:f1, ..., :fn]   -> array

Adds to the fields allowed in Jiak interactions.

Returns an array of added fields.

Raise JiakDataException if the fields include jiak.



121
122
123
# File 'lib/riakrest/core/jiak_data.rb', line 121

def allow(*fields)
  expand_schema("allow",*fields)
end

#attr_accessor(*fields) ⇒ Object

:call-seq:

attr_accessor :f1,...,:fn

Add read/write accessible fields.



79
80
81
82
# File 'lib/riakrest/core/jiak_data.rb', line 79

def attr_accessor(*fields)
  readable *fields
  writable *fields
end

#attr_converter(hash) ⇒ Object

:call-seq:

att_converter(hash)  -> nil

Specify a hash of optional Procs for converting data attribute values during reading and writing data to a Jiak server. Each hash key should be a data class attribute, with an associated value of a hash containing a write and/or a read Proc for use in converting data. Each Procs should accept one argument, the data value actually stored in Riak.

Example

A Person data class that stores a Date in ordinal format:

def PersonData

include JiakData
attr_accessor :name, :birthdate
attr_converter(:birthdate => {
  :write => lambda {|v| { :year => v.year, :yday => v.yday} },
  :read  => lambda {|v| Date::ordinal(v['year'],v['yday'])} } )

end



105
106
107
108
109
110
# File 'lib/riakrest/core/jiak_data.rb', line 105

def attr_converter(hash)
  hash.each do |attr,blks|
    @read_converter[attr]  = blks[:read]   if(blks[:read])
    @write_converter[attr] = blks[:write]  if(blks[:write])
  end
end

#attr_reader(*fields) ⇒ Object Also known as: attr

:call-seq:

attr_reader :f1,...,:fn

Add read accessible fields.



60
61
62
63
# File 'lib/riakrest/core/jiak_data.rb', line 60

def attr_reader(*fields)
  readable *fields
  nil
end

#attr_writer(*fields) ⇒ Object

:call-seq:

attr_writer :f1,...,:fn

Add write accessible fields.



70
71
72
73
# File 'lib/riakrest/core/jiak_data.rb', line 70

def attr_writer(*fields)
  writable *fields
  nil
end

#jiak_create(jiak) ⇒ Object

Use optional read converters to convert returned data values before passing to the JiakData constructor.



213
214
215
216
217
218
219
# File 'lib/riakrest/core/jiak_data.rb', line 213

def jiak_create(jiak)     # :nodoc:
  read_converter.each do |attr,blk|
    key = attr.to_s
    jiak[key] = blk.call(jiak[key])
  end
  new(jiak)
end

#keygen(&block) ⇒ Object

:call-seq:

JiakData.keygen(&block)  -> nil

Specify the key generation for an instance of the created JiakData class. Key generation will call the specified block in the scope of the current instance.



207
208
209
# File 'lib/riakrest/core/jiak_data.rb', line 207

def keygen(&block)
  define_method(:keygen,&block)
end

#readable(*fields) ⇒ Object

:call-seq:

readable :f1, ..., :fn  -> array
readable [:f1, ..., :fn]  -> array

Adds to the fields that can be read from Jiak.

Returns an array of added fields.



143
144
145
# File 'lib/riakrest/core/jiak_data.rb', line 143

def readable(*fields)
  expand_schema("readable",*fields)
end

#readwrite(*fields) ⇒ Object

:call-seq:

readwrite :f1, ..., :fn  -> nil
readwrite [:f1, ..., :fn]  -> nil

Adds to the fields that can be read and written.

Returns nil



165
166
167
168
169
# File 'lib/riakrest/core/jiak_data.rb', line 165

def readwrite(*fields)
  readable(*fields)
  writable(*fields)
  nil
end

#require(*fields) ⇒ Object

:call-seq:

require :f1, ..., :fn  -> array
require [:f1, ..., :fn]   -> array

Adds to the fields required during in Jiak interactions.

Returns an array of added fields.



132
133
134
# File 'lib/riakrest/core/jiak_data.rb', line 132

def require(*fields)
  expand_schema("require",*fields)
end

#schemaObject

:call-seq:

JiakData.schema  -> JiakSchema

Get a JiakSchema representation this data.



197
198
199
# File 'lib/riakrest/core/jiak_data.rb', line 197

def schema
  @schema ||= JiakSchema.new
end

#writable(*fields) ⇒ Object

:call-seq:

writable :f1, ..., :fn  -> arry
writable [:f1, ..., :fn]  -> arry

Adds to the fields that can be written to Jiak.

Returns an array of added fields.



154
155
156
# File 'lib/riakrest/core/jiak_data.rb', line 154

def writable(*fields)
  expand_schema("writable",*fields)
end