Class: Lightly
- Inherits:
-
Object
- Object
- Lightly
- Defined in:
- lib/lightly/lightly.rb,
lib/lightly/version.rb
Constant Summary collapse
- VERSION =
"0.1.2"
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
- #get(key, &block) ⇒ Object
-
#initialize(opts = {}) ⇒ Lightly
constructor
A new instance of Lightly.
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
31 32 33 34 |
# File 'lib/lightly/lightly.rb', line 31 def cached?(key) path = get_path key File.exist? path and !expired? path end |
#disable ⇒ Object
40 41 42 |
# File 'lib/lightly/lightly.rb', line 40 def disable @enabled = false end |
#enable ⇒ Object
36 37 38 |
# File 'lib/lightly/lightly.rb', line 36 def enable @enabled = true end |
#enabled? ⇒ Boolean
27 28 29 |
# File 'lib/lightly/lightly.rb', line 27 def enabled? @enabled end |
#flush ⇒ Object
22 23 24 25 |
# File 'lib/lightly/lightly.rb', line 22 def flush return false if dir == '/' || dir.empty? FileUtils.rm_rf dir end |
#get(key, &block) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/lightly/lightly.rb', line 14 def get(key, &block) return load key if cached?(key) && enabled? content = block.call save key, content if enabled? content end |