Module: Gretel::Trails
- Extended by:
- Resettable
- Defined in:
- lib/gretel/trails.rb,
lib/gretel/trails/engine.rb,
lib/gretel/trails/version.rb,
lib/gretel/trails/stores/store.rb,
lib/gretel/trails/stores/url_store.rb,
lib/gretel/trails/stores/redis_store.rb,
lib/gretel/trails/stores/active_record_store.rb,
lib/gretel/trails/strategies/hidden_strategy.rb
Defined Under Namespace
Modules: HiddenStrategy Classes: ActiveRecordStore, Engine, RedisStore, Store, UrlStore
Constant Summary collapse
- DEFAULT_STORES =
{ url: UrlStore, db: ActiveRecordStore, redis: RedisStore }
- VERSION =
"0.0.5"
Class Attribute Summary collapse
-
.trail_param ⇒ Object
Name of trail param.
Class Method Summary collapse
-
.available_stores ⇒ Object
Hash of registered stores.
-
.configure {|_self| ... } ⇒ Object
Yields
self
for configuration. -
.count ⇒ Object
Returns the current number of trails in the store.
-
.decode(key) ⇒ Object
Uses the store to decode a unique key to an array of links.
-
.delete_all ⇒ Object
Deletes all trails in the store.
-
.delete_expired ⇒ Object
Deletes expired keys from the store.
-
.encode(links) ⇒ Object
Uses the store to encode an array of links to a unique key that can be used in URLs.
-
.register_store(key, klass) ⇒ Object
Registers a store for use with ‘Gretel::Trails.store = :key`.
-
.store ⇒ Object
Gets the store that is used to encode and decode trails.
-
.store=(value) ⇒ Object
Sets the store that is used to encode and decode trails.
-
.strategies ⇒ Object
Activated strategies.
-
.strategies=(value) ⇒ Object
(also: strategy=)
Activates strategies.
Class Attribute Details
.trail_param ⇒ Object
Name of trail param. Default: :trail
.
94 95 96 |
# File 'lib/gretel/trails.rb', line 94 def trail_param @trail_param ||= :trail end |
Class Method Details
.available_stores ⇒ Object
Hash of registered stores.
61 62 63 |
# File 'lib/gretel/trails.rb', line 61 def available_stores @available_stores ||= DEFAULT_STORES end |
.configure {|_self| ... } ⇒ Object
107 108 109 |
# File 'lib/gretel/trails.rb', line 107 def configure yield self end |
.count ⇒ Object
Returns the current number of trails in the store. Not all stores support counting keys, and will raise an exception if they don’t.
83 84 85 |
# File 'lib/gretel/trails.rb', line 83 def count store.key_count end |
.decode(key) ⇒ Object
Uses the store to decode a unique key to an array of links.
71 72 73 |
# File 'lib/gretel/trails.rb', line 71 def decode(key) store.decode(key) end |
.delete_all ⇒ Object
Deletes all trails in the store. Not all stores support deleting trails, and will raise an exception if they don’t.
89 90 91 |
# File 'lib/gretel/trails.rb', line 89 def delete_all store.delete_all_keys end |
.delete_expired ⇒ Object
Deletes expired keys from the store. Not all stores support expiring keys, and will raise an exception if they don’t.
77 78 79 |
# File 'lib/gretel/trails.rb', line 77 def delete_expired store.delete_expired end |
.encode(links) ⇒ Object
Uses the store to encode an array of links to a unique key that can be used in URLs.
66 67 68 |
# File 'lib/gretel/trails.rb', line 66 def encode(links) store.encode(links) end |
.register_store(key, klass) ⇒ Object
56 57 58 |
# File 'lib/gretel/trails.rb', line 56 def register_store(key, klass) available_stores[key] = klass end |
.store ⇒ Object
Gets the store that is used to encode and decode trails. Default: Gretel::Trails::UrlStore
37 38 39 |
# File 'lib/gretel/trails.rb', line 37 def store @store ||= UrlStore end |
.store=(value) ⇒ Object
Sets the store that is used to encode and decode trails. Can be a subclass of Gretel::Trails::Store
, or a symbol: :url
, :db
, or :redis
.
43 44 45 46 47 48 49 50 51 |
# File 'lib/gretel/trails.rb', line 43 def store=(value) if value.is_a?(Symbol) klass = available_stores[value] raise ArgumentError, "Unknown Gretel::Trails.store #{value.inspect}. Use any of #{available_stores.inspect}." unless klass self.store = klass else @store = value end end |
.strategies ⇒ Object
Activated strategies
20 21 22 |
# File 'lib/gretel/trails.rb', line 20 def strategies @strategies ||= [] end |
.strategies=(value) ⇒ Object Also known as: strategy=
Activates strategies. Can be either a symbol or array of symbols.
Gretel::Trails.strategies = :hidden
Gretel::Trails.strategies = [:hidden, :other_strategy]
28 29 30 31 |
# File 'lib/gretel/trails.rb', line 28 def strategies=(value) @strategies = [value].flatten @strategies.each { |s| require "gretel/trails/strategies/#{s}_strategy"} end |