Module: OmnivorousEtag

Defined in:
lib/omnivorous_etag.rb

Defined Under Namespace

Classes: EtagCannotBeNil

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_version(object) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/omnivorous_etag.rb', line 35

def self.extract_version(object)
  if object.respond_to?(:new_record) && object.new_record?
    "new"
  # http://rubydoc.info/gems/acts_as_revisable
  elsif object.respond_to?(:revision_number)
    object.revision_number
  # http://rubydoc.info/gems/vestal_versions
  # http://rubydoc.info/gems/acts_as_versioned
  elsif object.respond_to?(:version)
    object.version
  end
  nil
end

.is_versioned?(object) ⇒ Boolean

Tells whether the passed AR record object is using a versioning plugin (in which case we can serve ourselves with the version tag it carries)

Returns:

  • (Boolean)


51
52
53
# File 'lib/omnivorous_etag.rb', line 51

def self.is_versioned?(object)
  extract_version(object) != nil
end

.package(blob) ⇒ Object



31
32
33
# File 'lib/omnivorous_etag.rb', line 31

def self.package(blob)
  Base64.encode64(blob)
end

Instance Method Details

#etag(anything) ⇒ Object

The etag method that takes anything, with the following behavior quirks

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/omnivorous_etag.rb', line 11

def etag(anything)
  raise EtagCannotBeNil, "Cannot generate ETags for nils" if anything.nil?
  
  data = if anything.respond_to?(:map)
    anything.map{|e| etag(e) }.join(";")
  elsif anything.respond_to?(:to_param) && OmnivorousEtag.is_versioned?(anything)
    etag([anything.class.to_s, anything.to_param, OmnivorousEtag.extract_version(anything)])
  elsif anything.is_a?(String) || anything.is_a?(Numeric) || anything.is_a?(Symbol)
    anything.to_s
  else
    Marshal.dump(anything)
  end
  
  if defined?(super)
    super(OmnivorousEtag.package(data))
  else
    OmnivorousEtag.package(data)
  end
end