Class: Lightly
- Inherits:
-
Object
- Object
- Lightly
- Defined in:
- lib/lightly/lightly.rb,
lib/lightly/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#life ⇒ Object
Returns the value of attribute life.
Instance Method Summary collapse
- #cached?(key) ⇒ Boolean
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #flush ⇒ Object
-
#initialize(opts = {}) ⇒ Lightly
constructor
A new instance of Lightly.
- #with(key, &block) ⇒ Object (also: #key)
Constructor Details
#initialize(opts = {}) ⇒ Lightly
Returns a new instance of Lightly.
7 8 9 10 11 12 |
# File 'lib/lightly/lightly.rb', line 7 def initialize(opts={}) @dir = opts[:dir] || 'cache' @life = opts[:life] || 3600 @hash = opts.key?(:hash) ? opts[:hash] : true @enabled = true end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
5 6 7 |
# File 'lib/lightly/lightly.rb', line 5 def dir @dir end |
#hash ⇒ Object
Returns the value of attribute hash.
5 6 7 |
# File 'lib/lightly/lightly.rb', line 5 def hash @hash end |
#life ⇒ Object
Returns the value of attribute life.
5 6 7 |
# File 'lib/lightly/lightly.rb', line 5 def life @life end |
Instance Method Details
#cached?(key) ⇒ Boolean
32 33 34 35 |
# File 'lib/lightly/lightly.rb', line 32 def cached?(key) path = get_path key File.exist? path and !expired? path end |
#disable ⇒ Object
41 42 43 |
# File 'lib/lightly/lightly.rb', line 41 def disable @enabled = false end |
#enable ⇒ Object
37 38 39 |
# File 'lib/lightly/lightly.rb', line 37 def enable @enabled = true end |
#enabled? ⇒ Boolean
28 29 30 |
# File 'lib/lightly/lightly.rb', line 28 def enabled? @enabled end |
#flush ⇒ Object
23 24 25 26 |
# File 'lib/lightly/lightly.rb', line 23 def flush return false if dir == '/' || dir.empty? FileUtils.rm_rf dir end |
#with(key, &block) ⇒ Object Also known as: key
14 15 16 17 18 19 20 |
# File 'lib/lightly/lightly.rb', line 14 def with(key, &block) return load key if cached?(key) && enabled? content = block.call save key, content if enabled? content end |