Class: Lightly

Inherits:
Object
  • Object
show all
Defined in:
lib/lightly/lightly.rb,
lib/lightly/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dirObject

Returns the value of attribute dir.



5
6
7
# File 'lib/lightly/lightly.rb', line 5

def dir
  @dir
end

#hashObject

Returns the value of attribute hash.



5
6
7
# File 'lib/lightly/lightly.rb', line 5

def hash
  @hash
end

#lifeObject

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

Returns:

  • (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

#disableObject



41
42
43
# File 'lib/lightly/lightly.rb', line 41

def disable
  @enabled = false
end

#enableObject



37
38
39
# File 'lib/lightly/lightly.rb', line 37

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lightly/lightly.rb', line 28

def enabled?
  @enabled
end

#flushObject



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