lightly 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d313f9708367bd12cd91e8a40edfead09bdab9abb923f1ba1b7cbd5129ff61e5
4
- data.tar.gz: 6b19b9bf03a20d1725c5a9ef018a1e75fb945860195b158052dc74a0a0cc3de9
3
+ metadata.gz: 1218707c4bb8d9de7bcc161073fde7a1f8af1e0f2539cec99e8600f14fa7f2b8
4
+ data.tar.gz: e06982d22591a13c917d8c91d91f70ad9606b0b4a19addea29a21c7db68b27cc
5
5
  SHA512:
6
- metadata.gz: 0b1836e246ed0b7d2e3212fd9a273f390df4f34c992b1817ba326e11f12a23303c02c40bd398a3d633eb37c0761a59b7b268c125ef6d3b472a436ff93345b52d
7
- data.tar.gz: 7023d499f4fa07b16c4580a0f6507970898cd507c097c1c7a88638174afe101c356e0435b290f78f0aedea8d578ccde2cea06fb3390ecf7b530275eaf4b74cf8
6
+ metadata.gz: d0e07e0f3b6a90c78b2a27564ab457b702a169cd5fcd4665d4bba39e7a2701817dc0896a5beacc911c8df2deb0cbbf26c04c29e57485efa782fd07b50fe213ce
7
+ data.tar.gz: 95d72071ede043c885fc9e86945a520d48697a4ac56af6b045739eafe711f4cb5d371cd3a557cb3263fe1a03c575a46c8f75832abc967c40afe06a386217b7a5
data/README.md CHANGED
@@ -69,12 +69,14 @@ operation inside the block, and save it to the cache object.
69
69
  By default, the cached objects are stored in the `./cache` directory, and
70
70
  expire after 60 minutes. The cache directory will be created as needed.
71
71
 
72
- In addition, the provided key is hashed to its MD5 representation.
72
+ In addition, the provided key is hashed to its MD5 representation, and the file
73
+ permissions are optionally set.
73
74
 
74
75
  You can change these settings on initialization:
75
76
 
76
77
  ```ruby
77
- lightly = Lightly.new dir: 'tmp/my_cache', life: 7200, hash: false
78
+ lightly = Lightly.new dir: 'tmp/my_cache', life: 7200,
79
+ hash: false, permissions: 0o640
78
80
  ```
79
81
 
80
82
  Or later:
@@ -84,6 +86,7 @@ lightly = Lightly.new
84
86
  lightly.dir = 'tmp/my_cache'
85
87
  lightly.life = '1d'
86
88
  lightly.hash = false
89
+ lightly.permissions = 0o640
87
90
  ```
88
91
 
89
92
  The `life` property accepts any of these formats:
@@ -3,19 +3,21 @@ require 'fileutils'
3
3
 
4
4
  class Lightly
5
5
  module CacheOperations
6
+ attr_accessor :permissions
6
7
  attr_writer :dir, :hash
7
8
 
8
- def initialize(dir: 'cache', life: '1h', hash: true, enabled: true)
9
+ def initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil)
9
10
  @dir = dir
10
11
  @life = life_to_seconds life
11
12
  @hash = hash
12
13
  @enabled = enabled
14
+ @permissions = permissions
13
15
  end
14
16
 
15
- def get(key, &block)
17
+ def get(key)
16
18
  return load key if cached?(key) && enabled?
17
19
 
18
- content = block.call
20
+ content = yield
19
21
  save key, content if content && enabled?
20
22
  content
21
23
  end
@@ -47,17 +49,19 @@ class Lightly
47
49
 
48
50
  def flush
49
51
  return false if dir == '/' || dir.empty?
52
+
50
53
  FileUtils.rm_rf dir
51
54
  end
52
55
 
53
56
  def prune
54
57
  return false if dir == '/' || dir.empty?
58
+
55
59
  Dir["#{dir}/*"].each { |file| expired? file }
56
60
  end
57
61
 
58
62
  def cached?(key)
59
63
  path = get_path key
60
- File.exist?(path) and File.size(path) > 0 and !expired?(path)
64
+ File.exist?(path) and File.size(path).positive? and !expired?(path)
61
65
  end
62
66
 
63
67
  def enable
@@ -76,11 +80,11 @@ class Lightly
76
80
  def save(key, content)
77
81
  FileUtils.mkdir_p dir
78
82
  path = get_path key
79
- File.open path, 'wb' do |f|
80
- f.write Marshal.dump content
83
+ File.open path, 'wb', permissions do |file|
84
+ file.write Marshal.dump(content)
81
85
  end
82
86
  end
83
-
87
+
84
88
  private
85
89
 
86
90
  def load(key)
@@ -97,13 +101,13 @@ class Lightly
97
101
  arg = arg.to_s
98
102
 
99
103
  case arg[-1]
100
- when 's'; arg[0..-1].to_i
101
- when 'm'; arg[0..-1].to_i * 60
102
- when 'h'; arg[0..-1].to_i * 60 * 60
103
- when 'd'; arg[0..-1].to_i * 60 * 60 * 24
104
- else; arg.to_i
104
+ when 's' then arg[0..].to_i
105
+ when 'm' then arg[0..].to_i * 60
106
+ when 'h' then arg[0..].to_i * 60 * 60
107
+ when 'd' then arg[0..].to_i * 60 * 60 * 24
108
+ else
109
+ arg.to_i
105
110
  end
106
111
  end
107
-
108
112
  end
109
113
  end
@@ -1,7 +1,7 @@
1
- class Lightly
2
- include CacheOperations
3
-
4
- class << self
5
- include CacheOperations
6
- end
7
- end
1
+ class Lightly
2
+ include CacheOperations
3
+
4
+ class << self
5
+ include CacheOperations
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  class Lightly
2
- VERSION = "0.3.3"
3
- end
2
+ VERSION = '0.4.0'
3
+ end
data/lib/lightly.rb CHANGED
@@ -1,4 +1,2 @@
1
1
  require 'lightly/cache_operations'
2
2
  require 'lightly/lightly'
3
-
4
- require 'byebug' if ENV['BYEBUG']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-12 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easy to use file cache
14
14
  email: db@dannyben.com
@@ -24,7 +24,8 @@ files:
24
24
  homepage: https://github.com/DannyBen/lightly
25
25
  licenses:
26
26
  - MIT
27
- metadata: {}
27
+ metadata:
28
+ rubygems_mfa_required: 'true'
28
29
  post_install_message:
29
30
  rdoc_options: []
30
31
  require_paths:
@@ -33,14 +34,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
34
  requirements:
34
35
  - - ">="
35
36
  - !ruby/object:Gem::Version
36
- version: 2.0.0
37
+ version: '3.0'
37
38
  required_rubygems_version: !ruby/object:Gem::Requirement
38
39
  requirements:
39
40
  - - ">="
40
41
  - !ruby/object:Gem::Version
41
42
  version: '0'
42
43
  requirements: []
43
- rubygems_version: 3.1.2
44
+ rubygems_version: 3.3.26
44
45
  signing_key:
45
46
  specification_version: 4
46
47
  summary: File cache for performing heavy tasks, lightly.