Class: Webmachine::ETag

Inherits:
Object
  • Object
show all
Includes:
QuotedString
Defined in:
lib/webmachine/etags.rb

Overview

A wrapper around entity tags that encapsulates their semantics. This class by itself represents a “strong” entity tag.

Direct Known Subclasses

WeakETag

Constant Summary collapse

WEAK_ETAG =

The pattern for a weak entity tag

/^W\/#{QUOTED_STRING}$/.freeze

Constants included from QuotedString

QuotedString::QS_ANCHORED, QuotedString::QUOTED_STRING

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QuotedString

#escape_quotes, #quote, #unescape_quotes, #unquote

Constructor Details

#initialize(etag) ⇒ ETag

Returns a new instance of ETag.



21
22
23
# File 'lib/webmachine/etags.rb', line 21

def initialize(etag)
  @etag = quote(etag)
end

Instance Attribute Details

#etagObject (readonly)

Returns the value of attribute etag.



19
20
21
# File 'lib/webmachine/etags.rb', line 19

def etag
  @etag
end

Class Method Details

.new(etag) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/webmachine/etags.rb', line 11

def self.new(etag)
  return etag if ETag === etag
  klass = etag =~ WEAK_ETAG ? WeakETag : self
  klass.send(:allocate).tap do |obj|
    obj.send(:initialize, etag)
  end
end

Instance Method Details

#==(other) ⇒ Object

An entity tag is equivalent to another entity tag if their quoted values are equivalent. It is also equivalent to a String which represents the equivalent ETag.



28
29
30
31
32
33
34
35
# File 'lib/webmachine/etags.rb', line 28

def ==(other)
  case other
  when ETag
    other.etag == @etag
  when String
    quote(other) == @etag
  end
end

#to_sObject

Converts the entity tag into a string appropriate for use in a header.



39
40
41
# File 'lib/webmachine/etags.rb', line 39

def to_s
  quote @etag
end