Class: Riakpb::Content
- Inherits:
-
Object
- Object
- Riakpb::Content
- Includes:
- Util::MessageCode, Util::Translation
- Defined in:
- lib/riakpb/content.rb
Overview
Parent class of all object types supported by ripple. RObject represents the data and metadata stored in a bucket/key pair in the Riakpb database.
Constant Summary
Constants included from Util::MessageCode
Util::MessageCode::DEL_REQUEST, Util::MessageCode::DEL_RESPONSE, Util::MessageCode::ERROR_RESPONSE, Util::MessageCode::GET_BUCKET_REQUEST, Util::MessageCode::GET_BUCKET_RESPONSE, Util::MessageCode::GET_CLIENT_ID_REQUEST, Util::MessageCode::GET_CLIENT_ID_RESPONSE, Util::MessageCode::GET_REQUEST, Util::MessageCode::GET_RESPONSE, Util::MessageCode::GET_SERVER_INFO_REQUEST, Util::MessageCode::GET_SERVER_INFO_RESPONSE, Util::MessageCode::LIST_BUCKETS_REQUEST, Util::MessageCode::LIST_BUCKETS_RESPONSE, Util::MessageCode::LIST_KEYS_REQUEST, Util::MessageCode::LIST_KEYS_RESPONSE, Util::MessageCode::MAP_REDUCE_REQUEST, Util::MessageCode::MAP_REDUCE_RESPONSE, Util::MessageCode::MC_RESPONSE_FOR, Util::MessageCode::PING_REQUEST, Util::MessageCode::PING_RESPONSE, Util::MessageCode::PUT_REQUEST, Util::MessageCode::PUT_RESPONSE, Util::MessageCode::RESPONSE_CLASS_FOR, Util::MessageCode::SET_BUCKET_REQUEST, Util::MessageCode::SET_BUCKET_RESPONSE, Util::MessageCode::SET_CLIENT_ID_REQUEST, Util::MessageCode::SET_CLIENT_ID_RESPONSE
Instance Attribute Summary collapse
-
#charset ⇒ String
The charset of the object.
-
#content_encoding ⇒ String
The content encoding of the object.
-
#content_type ⇒ String
The MIME content type of the object.
-
#key ⇒ Key
The key in which this Content is stored.
-
#last_mod ⇒ Time
The Last-Modified header from the most recent HTTP response, useful for caching and reloading.
-
#last_mod_usecs ⇒ Time
The Last-Modified header from the most recent HTTP response, useful for caching and reloading.
-
#links ⇒ Set<Link>
An Set of Link objects for relationships between this object and other resources.
-
#options ⇒ Object
Returns the value of attribute options.
-
#usermeta ⇒ Hash
A hash of any user-supplied metadata, consisting of a key/value pair.
-
#value ⇒ String
The data stored in Riakpb at this object’s key.
-
#vtag ⇒ String
The vtag of the object.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
-
#initialize(key, options = {}) {|_self| ... } ⇒ Content
constructor
A new instance of Content.
-
#inspect ⇒ String
A representation suitable for IRB and debugging output.
-
#link_key(tags) ⇒ Hash
(also: #link)
Internalizes a link to a Key, which will be saved on next call to…
-
#load(content) ⇒ Content
Load information for the content from the response object, Riakpb::RpbContent.
- #merge(content) ⇒ Object
-
#save(options = {}) ⇒ Object
Save the Content instance in riak.
-
#save!(options = {}) ⇒ Object
Save the Content instance in riak.
-
#to_pb ⇒ Riakpb::RpbContent
An instance of a RpbContent, suitable for protobuf exchange.
Methods included from Util::Translation
Constructor Details
#initialize(key, options = {}) {|_self| ... } ⇒ Content
Returns a new instance of Content.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/riakpb/content.rb', line 52 def initialize(key, ={}) @key = key unless key.nil? @links = Hash.new{|k,v| k[v] = []} @usermeta = {} @options = self.last_mod = Time.now yield self if block_given? end |
Instance Attribute Details
#charset ⇒ String
Returns the charset of the object.
22 23 24 |
# File 'lib/riakpb/content.rb', line 22 def charset @charset end |
#content_encoding ⇒ String
Returns the content encoding of the object.
25 26 27 |
# File 'lib/riakpb/content.rb', line 25 def content_encoding @content_encoding end |
#content_type ⇒ String
Returns the MIME content type of the object.
19 20 21 |
# File 'lib/riakpb/content.rb', line 19 def content_type @content_type end |
#key ⇒ Key
Returns the key in which this Content is stored.
12 13 14 |
# File 'lib/riakpb/content.rb', line 12 def key @key end |
#last_mod ⇒ Time
Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.
34 35 36 |
# File 'lib/riakpb/content.rb', line 34 def last_mod @last_mod end |
#last_mod_usecs ⇒ Time
Returns the Last-Modified header from the most recent HTTP response, useful for caching and reloading.
38 39 40 |
# File 'lib/riakpb/content.rb', line 38 def last_mod_usecs @last_mod_usecs end |
#links ⇒ Set<Link>
Returns an Set of Link objects for relationships between this object and other resources.
31 32 33 |
# File 'lib/riakpb/content.rb', line 31 def links @links end |
#options ⇒ Object
Returns the value of attribute options.
45 46 47 |
# File 'lib/riakpb/content.rb', line 45 def @options end |
#usermeta ⇒ Hash
Returns a hash of any user-supplied metadata, consisting of a key/value pair.
42 43 44 |
# File 'lib/riakpb/content.rb', line 42 def @usermeta end |
#value ⇒ String
Returns the data stored in Riakpb at this object’s key. Varies in format by content-type.
15 16 17 |
# File 'lib/riakpb/content.rb', line 15 def value @value end |
#vtag ⇒ String
Returns the vtag of the object.
28 29 30 |
# File 'lib/riakpb/content.rb', line 28 def vtag @vtag end |
Instance Method Details
#[](attribute) ⇒ Object
63 64 65 |
# File 'lib/riakpb/content.rb', line 63 def [](attribute) instance_variable_get "@#{attribute}" end |
#inspect ⇒ String
Returns A representation suitable for IRB and debugging output.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/riakpb/content.rb', line 259 def inspect "#<#Riakpb::Content " + [ (@value.nil?) ? nil : "value=#{@value.inspect}", (@content_type.nil?) ? nil : "content_type=#{@content_type.inspect}", (@charset.nil?) ? nil : "charset=#{@charset.inspect}", (@content_encoding.nil?) ? nil : "content_encoding=#{@content_encoding.inspect}", (@vtag.nil?) ? nil : "vtag=#{@vtag.inspect}", (@links.nil?) ? nil : "links=#{@_links.inspect}", (@last_mod.nil?) ? nil : "last_mod=#{last_mod.inspect}", (@last_mod_usecs.nil?) ? nil : "last_mod_usecs=#{last_mod_usecs.inspect}", (@usermeta.nil?) ? nil : "usermeta=#{@usermeta.inspect}" ].compact.join(", ") + ">" end |
#link_key(tags) ⇒ Hash Also known as: link
Internalizes a link to a Key, which will be saved on next call to… uh, save
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/riakpb/content.rb', line 152 def link_key() raise TypeError.new t('invalid_tag') unless .is_a?(Hash) or .is_a?(Array) .each do |tag, link| case link when Array newlink = Riakpb::RpbLink.new newlink[:bucket] = link[0] newlink[:key] = link[1] raise ArgumentError.new t('invalid_tag') if link[0].nil? or link[1].nil? @links[tag.to_s] << newlink when Riakpb::Key @links[tag.to_s] << link else raise TypeError.new t('invalid_tag') end end # tags.each do |tag, link| end |
#load(content) ⇒ Content
Load information for the content from the response object, Riakpb::RpbContent.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/riakpb/content.rb', line 70 def load(content) case content when Riakpb::RpbContent, Hash, Riakpb::Content last_mod = content[:last_mod] last_mod_usecs = content[:last_mod_usecs] if not (last_mod.blank? or last_mod_usecs.blank?) self.last_mod = Time.at "#{last_mod}.#{last_mod_usecs}".to_f end @content_type = content[:content_type] unless content[:content_type].blank? @charset = content[:charset] unless content[:charset].blank? @content_encoding = content[:content_encoding] unless content[:content_encoding].blank? @vtag = content[:vtag] unless content[:vtag].blank? self.links = content[:links] unless content[:links].blank? self. = content[:usermeta] unless content[:usermeta].blank? unless content[:value].blank? case @content_type when /json/ @value = ActiveSupport::JSON.decode(content[:value]) when /octet/ @value = Marshal.load(content[:value]) else @value = content[:value] end end else raise ArgumentError, t("riak_content_type") end return(self) end |
#merge(content) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/riakpb/content.rb', line 103 def merge(content) @links.merge( self.links = content[:links] ) unless content[:links].blank? @usermega.merge( self. = content[:usermeta] ) unless content[:usermeta].blank? @content_type = content[:content_type] unless content[:content_type].blank? @value = content[:value] @charset = content[:charset] unless content[:charset].blank? @content_encoding = content[:content_encoding] unless content[:content_encoding].blank? @vtag = content[:vtag] unless content[:vtag].blank? @last_mod = content[:last_mod] @last_mod_usecs = content[:last_mod_usecs] return(self) end |
#save(options = {}) ⇒ Object
Save the Content instance in riak.
127 128 129 130 131 132 133 134 |
# File 'lib/riakpb/content.rb', line 127 def save(={}) begin self.save!() rescue FailedRequest return(false) end return(true) end |
#save!(options = {}) ⇒ Object
Save the Content instance in riak. Raise/do not rescue on failure.
140 141 142 143 144 145 146 |
# File 'lib/riakpb/content.rb', line 140 def save!(={}) [:content] = self = @options.merge() return(true) if @key.save() return(false) # Create and raise Error message for this? Extend "Failed Request"? end |
#to_pb ⇒ Riakpb::RpbContent
Returns An instance of a RpbContent, suitable for protobuf exchange.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/riakpb/content.rb', line 210 def to_pb rpb_content = Riakpb::RpbContent.new rpb_links = [] @links.each do |tag, links| links.each do |link| case link when Riakpb::RpbLink rpb_links << hlink when Riakpb::Key pb_link = link.to_pb_link pb_link.tag = tag rpb_links << pb_link end end end = [] @usermeta.each do |key,value| pb_pair = Riakpb::RpbPair.new pb_pair[:key] = key pb_pair[:value] = value << pb_pair end catch(:redo) do case @content_type when /octet/ rpb_content.value = Marshal.dump(@value) unless @value.blank? when /json/ rpb_content.value = ActiveSupport::JSON.encode(@value) unless @value.blank? when "", nil @content_type = "application/json" redo else rpb_content.value = @value.to_s unless @value.blank? end end rpb_content.content_type = @content_type unless @content_type.blank? rpb_content.charset = @charset unless @charset.blank? # || @charset.empty? rpb_content.content_encoding = @content_encoding unless @content_encoding.blank? # || @content_encoding.empty? rpb_content.links = rpb_links unless rpb_links.blank? rpb_content. = unless .blank? return(rpb_content) end |