Class: Riak::RContent
Overview
Represents single (potentially-conflicted) value stored against a key in the Riak database. This includes the raw value as well as metadata.
Instance Attribute Summary collapse
-
#content_type ⇒ String
The MIME content type of the value.
-
#etag ⇒ String
The ETag header from the most recent HTTP response, useful for caching and reloading.
-
#indexes ⇒ Hash<Set>
A hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index.
-
#last_modified ⇒ Time
The Last-Modified header from the most recent HTTP response, useful for caching and reloading.
-
#links ⇒ Set<Link>
A Set of Link objects for relationships between this object and other resources.
-
#meta ⇒ Hash
A hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.
-
#robject ⇒ Riak::RObject
The RObject to which this sibling belongs.
Instance Method Summary collapse
-
#data ⇒ Object
The unmarshaled form of #raw_data stored in riak at this object’s key.
-
#data=(new_data) ⇒ Object
The object stored.
-
#deserialize(body) ⇒ Object
Deserializes the internal object data from a Riak response.
-
#initialize(robject) { ... } ⇒ RContent
constructor
Creates a new object value.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
- #load_map_reduce_value(hash) ⇒ Object private
-
#raw_data ⇒ String
Raw data stored in riak for this object’s key.
-
#raw_data=(new_raw_data) ⇒ String
The data stored.
-
#serialize(payload) ⇒ Object
Serializes the internal object data for sending to Riak.
Methods included from Util::Translation
Constructor Details
#initialize(robject) { ... } ⇒ RContent
Creates a new object value. This should not normally need to be called by users of the client. Normal, single-value use can rely on the delegating accessors on Riak::RObject.
47 48 49 50 51 52 |
# File 'lib/riak/rcontent.rb', line 47 def initialize(robject) @robject = robject @links, @meta = Set.new, {} @indexes = new_index_hash yield self if block_given? end |
Instance Attribute Details
#content_type ⇒ String
Returns the MIME content type of the value.
18 19 20 |
# File 'lib/riak/rcontent.rb', line 18 def content_type @content_type end |
#etag ⇒ String
Returns the ETag header from the most recent HTTP response, useful for caching and reloading.
24 25 26 |
# File 'lib/riak/rcontent.rb', line 24 def etag @etag end |
#indexes ⇒ Hash<Set>
Returns a hash of secondary indexes, where the key is the index name and the value is a Set of index entries for that index.
35 36 37 |
# File 'lib/riak/rcontent.rb', line 35 def indexes @indexes end |
#last_modified ⇒ Time
Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.
27 28 29 |
# File 'lib/riak/rcontent.rb', line 27 def last_modified @last_modified end |
#links ⇒ Set<Link>
Returns a Set of Link objects for relationships between this object and other resources.
21 22 23 |
# File 'lib/riak/rcontent.rb', line 21 def links @links end |
#meta ⇒ Hash
Returns a hash of any X-Riak-Meta-* headers that were in the HTTP response, keyed on the trailing portion.
30 31 32 |
# File 'lib/riak/rcontent.rb', line 30 def @meta end |
#robject ⇒ Riak::RObject
Returns the RObject to which this sibling belongs.
38 39 40 |
# File 'lib/riak/rcontent.rb', line 38 def robject @robject end |
Instance Method Details
#data ⇒ Object
Returns the unmarshaled form of #raw_data stored in riak at this object’s key.
62 63 64 65 66 67 68 69 |
# File 'lib/riak/rcontent.rb', line 62 def data if @raw_data && !@data raw = @raw_data.respond_to?(:read) ? @raw_data.read : @raw_data @data = deserialize(raw) @raw_data = nil end @data end |
#data=(new_data) ⇒ Object
Returns the object stored.
76 77 78 79 80 81 82 83 |
# File 'lib/riak/rcontent.rb', line 76 def data=(new_data) if new_data.respond_to?(:read) raise ArgumentError.new(t("invalid_io_object")) end @raw_data = nil @data = new_data end |
#deserialize(body) ⇒ Object
Deserializes the internal object data from a Riak response. Differs based on the content-type. This method is called internally when loading the object. Automatically deserialized formats:
-
JSON (application/json)
-
YAML (text/yaml)
-
Marshal (application/x-ruby-marshal)
124 125 126 |
# File 'lib/riak/rcontent.rb', line 124 def deserialize(body) Serializers.deserialize(@content_type, body) end |
#inspect ⇒ String
Returns A representation suitable for IRB and debugging output.
129 130 131 132 133 134 135 136 |
# File 'lib/riak/rcontent.rb', line 129 def inspect body = if @data || Serializers[content_type] data.inspect else @raw_data && "(#{@raw_data.size} bytes)" end "#<#{self.class.name} [#{@content_type}]:#{body}>" end |
#load_map_reduce_value(hash) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/riak/rcontent.rb', line 139 def load_map_reduce_value(hash) = hash['metadata'] extract_if_present(, 'X-Riak-VTag', :etag) extract_if_present(, 'content-type', :content_type) extract_if_present(, 'X-Riak-Last-Modified', :last_modified) { |v| Time.httpdate( v ) } extract_if_present(, 'index', :indexes) do |entries| Hash[ entries.map {|k, v| [k, Set.new(Array(v))] } ] end extract_if_present(, 'Links', :links) do |links| Set.new( links.map { |l| Link.new(*l) } ) end extract_if_present(, 'X-Riak-Meta', :meta) do || Hash[ .map do |k, v| [k.sub(%r{^x-riak-meta-}i, ''), [v]] end ] end extract_if_present(hash, 'data', :data) { |v| deserialize(v) } end |
#raw_data ⇒ String
Returns raw data stored in riak for this object’s key.
87 88 89 90 91 92 93 |
# File 'lib/riak/rcontent.rb', line 87 def raw_data if @data && !@raw_data @raw_data = serialize(@data) @data = nil end @raw_data end |
#raw_data=(new_raw_data) ⇒ String
Returns the data stored.
99 100 101 102 |
# File 'lib/riak/rcontent.rb', line 99 def raw_data=(new_raw_data) @data = nil @raw_data = new_raw_data end |
#serialize(payload) ⇒ Object
Serializes the internal object data for sending to Riak. Differs based on the content-type. This method is called internally when storing the object. Automatically serialized formats:
-
JSON (application/json)
-
YAML (text/yaml)
-
Marshal (application/x-ruby-marshal)
When given an IO-like object (e.g. File), no serialization will be done.
113 114 115 |
# File 'lib/riak/rcontent.rb', line 113 def serialize(payload) Serializers.serialize(@content_type, payload) end |