Class: Alchemy::CacheControl
- Inherits:
-
Object
- Object
- Alchemy::CacheControl
- Defined in:
- app/models/alchemy/cache_control.rb
Overview
Normalized HTTP Cache-Control directives derived from a page layout cache
or element page_cache config value. Combine two values with #& to get the
most restrictive of the two — this is how the page layout and its elements
are merged (elements can only tighten, never loosen).
Constant Summary collapse
- VISIBILITIES =
[:public, :private].freeze
Instance Attribute Summary collapse
-
#max_age ⇒ Object
readonly
Returns the value of attribute max_age.
-
#visibility ⇒ Object
readonly
Returns the value of attribute visibility.
Class Method Summary collapse
Instance Method Summary collapse
-
#&(other) ⇒ Object
Most restrictive of self and other.
- #==(other) ⇒ Object (also: #eql?)
- #default? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(no_store: false, no_cache: false, max_age: nil, visibility: :public, default: false) ⇒ CacheControl
constructor
A new instance of CacheControl.
- #no_cache? ⇒ Boolean
- #no_store? ⇒ Boolean
- #private? ⇒ Boolean
-
#public? ⇒ Boolean
Whether the response is flagged
publicto HTTP caches. - #restrict_visibility ⇒ Object
Constructor Details
#initialize(no_store: false, no_cache: false, max_age: nil, visibility: :public, default: false) ⇒ CacheControl
Returns a new instance of CacheControl.
37 38 39 40 41 42 43 |
# File 'app/models/alchemy/cache_control.rb', line 37 def initialize(no_store: false, no_cache: false, max_age: nil, visibility: :public, default: false) @no_store = no_store @no_cache = no_cache @max_age = max_age @visibility = visibility @default = default end |
Instance Attribute Details
#max_age ⇒ Object (readonly)
Returns the value of attribute max_age.
35 36 37 |
# File 'app/models/alchemy/cache_control.rb', line 35 def max_age @max_age end |
#visibility ⇒ Object (readonly)
Returns the value of attribute visibility.
35 36 37 |
# File 'app/models/alchemy/cache_control.rb', line 35 def visibility @visibility end |
Class Method Details
.parse(value) ⇒ CacheControl
13 14 15 16 17 18 19 20 21 22 |
# File 'app/models/alchemy/cache_control.rb', line 13 def self.parse(value) case value when CacheControl then value when nil, true then new(default: true) when false then new(no_store: true) when Integer then new(max_age: value) when Hash then from_hash(value) else new(default: true) end end |
Instance Method Details
#&(other) ⇒ Object
Most restrictive of self and other.
69 70 71 72 73 74 75 76 77 |
# File 'app/models/alchemy/cache_control.rb', line 69 def &(other) return self if other.nil? return self.class.new(no_store: true) if no_store? || other.no_store? vis = (private? || other.private?) ? :private : :public return self.class.new(no_cache: true, visibility: vis) if no_cache? || other.no_cache? self.class.new(visibility: vis, max_age: min_max_age(other)) end |
#==(other) ⇒ Object Also known as: eql?
85 86 87 88 89 90 91 |
# File 'app/models/alchemy/cache_control.rb', line 85 def ==(other) other.is_a?(CacheControl) && no_store? == other.no_store? && no_cache? == other.no_cache? && max_age == other.max_age && visibility == other.visibility end |
#default? ⇒ Boolean
53 54 55 |
# File 'app/models/alchemy/cache_control.rb', line 53 def default? @default end |
#hash ⇒ Object
94 95 96 |
# File 'app/models/alchemy/cache_control.rb', line 94 def hash [no_store?, no_cache?, max_age, visibility].hash end |
#no_cache? ⇒ Boolean
49 50 51 |
# File 'app/models/alchemy/cache_control.rb', line 49 def no_cache? @no_cache end |
#no_store? ⇒ Boolean
45 46 47 |
# File 'app/models/alchemy/cache_control.rb', line 45 def no_store? @no_store end |
#private? ⇒ Boolean
64 65 66 |
# File 'app/models/alchemy/cache_control.rb', line 64 def private? visibility == :private end |
#public? ⇒ Boolean
Whether the response is flagged public to HTTP caches. A no-cache
response is never public: Rails only pairs no-cache with public, and
"public" adds nothing to an always-revalidated response.
60 61 62 |
# File 'app/models/alchemy/cache_control.rb', line 60 def public? visibility == :public && !no_cache? end |
#restrict_visibility ⇒ Object
79 80 81 82 83 |
# File 'app/models/alchemy/cache_control.rb', line 79 def restrict_visibility return self if no_store? || private? self.class.new(no_cache: no_cache?, max_age: max_age, visibility: :private) end |