s3_index 0.1.1 → 0.2.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 +4 -4
- data/lib/s3_index.rb +7 -0
- data/lib/s3_index/accessor.rb +120 -0
- data/lib/s3_index/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ebc8919d29a2d7a697050a9c17a1255899ce551
|
4
|
+
data.tar.gz: 852120e458e1bfd9a651df91a70ce27c032e400d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5605f6707ce1c1a962df8ce94ead265036de6b2317dbf5905e7c49c0159cbe63977fabe3138f6d10e33558802be1d2dc2955dac0e5ee7a53aafb98fca273f124
|
7
|
+
data.tar.gz: a3ce408ccea48e347970e5ddccc495eb47cfe273a63ac4b0362dfda5fba2b1499bc97d96c99ad1d18cad71f452244dd128008bb5eabdfbaae7a103fcaa2f7ad2
|
data/lib/s3_index.rb
CHANGED
@@ -6,6 +6,7 @@ module S3Index
|
|
6
6
|
extend ActiveSupport::Autoload
|
7
7
|
|
8
8
|
autoload :Index
|
9
|
+
autoload :Accessor
|
9
10
|
|
10
11
|
module_function
|
11
12
|
|
@@ -98,6 +99,12 @@ module S3Index
|
|
98
99
|
def default_client=(client)
|
99
100
|
@s3_client = client
|
100
101
|
end
|
102
|
+
|
103
|
+
# Let the model handle everything else
|
104
|
+
def method_missing(meth, *args, &block)
|
105
|
+
super unless Index.respond_to?(meth)
|
106
|
+
Index.public_send(meth, *args, &block)
|
107
|
+
end
|
101
108
|
end
|
102
109
|
|
103
110
|
S3Index.eager_load!
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# module
|
2
|
+
module S3Index
|
3
|
+
# Adds S3 backed data accessors to any ActiveRecord model
|
4
|
+
#
|
5
|
+
# ## Example:
|
6
|
+
# ```
|
7
|
+
# class Foo < ActiveRecord::Base
|
8
|
+
# include S3Index::Accessor
|
9
|
+
#
|
10
|
+
# s3_index_accessor :s3_data,
|
11
|
+
# cache_dir: '/tmp'
|
12
|
+
# bucket: 'foo',
|
13
|
+
# s3_resource: Aws::S3::Resource.new
|
14
|
+
# end
|
15
|
+
# ```
|
16
|
+
module Accessor
|
17
|
+
extend ActiveSupport::Concern
|
18
|
+
|
19
|
+
included do
|
20
|
+
class_attribute :_s3_index_config
|
21
|
+
end
|
22
|
+
|
23
|
+
class_methods do
|
24
|
+
def s3_index_accessor(name, cache_dir:, bucket: nil, s3_resource: nil)
|
25
|
+
# IMPORTANT never move this line unless you want everything to break.
|
26
|
+
# This ensures lowest descendent has its own hash instance.
|
27
|
+
self._s3_index_config ||= {}
|
28
|
+
|
29
|
+
# do the assignment
|
30
|
+
self._s3_index_config[name] = {
|
31
|
+
name: name,
|
32
|
+
cache_dir: cache_dir,
|
33
|
+
bucket: bucket || name.to_s,
|
34
|
+
s3_resource: s3_resource || Aws::S3::Resource.new
|
35
|
+
}
|
36
|
+
|
37
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
38
|
+
after_save do |row|
|
39
|
+
handle_s3_index_after_save(:#{name}, _s3_index_config[:#{name}], row)
|
40
|
+
end
|
41
|
+
RUBY
|
42
|
+
|
43
|
+
class_eval(<<-RUBY.strip_heredoc, __FILE__, __LINE__)
|
44
|
+
def #{name}=(value)
|
45
|
+
handle_s3_index_write(:#{name}, _s3_index_config[:#{name}], value)
|
46
|
+
end
|
47
|
+
RUBY
|
48
|
+
|
49
|
+
class_eval(<<-RUBY.strip_heredoc, __FILE__, __LINE__)
|
50
|
+
def #{name}(force: false)
|
51
|
+
handle_s3_index_read(:#{name}, _s3_index_config[:#{name}], force: force)
|
52
|
+
end
|
53
|
+
RUBY
|
54
|
+
|
55
|
+
class_eval(<<-RUBY.strip_heredoc, __FILE__, __LINE__)
|
56
|
+
def #{name}!
|
57
|
+
#{name}(force: true)
|
58
|
+
end
|
59
|
+
RUBY
|
60
|
+
|
61
|
+
class_eval(<<-RUBY.strip_heredoc, __FILE__, __LINE__)
|
62
|
+
def #{name}_index
|
63
|
+
handle_s3_index_record(:#{name}, _s3_index_config[:#{name}])
|
64
|
+
end
|
65
|
+
RUBY
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def handle_s3_index_write(name, config, value)
|
72
|
+
File.open(full_cache_path(config), 'wb') { |f| f << value } if self.id
|
73
|
+
instance_variable_set("@_#{name}", value)
|
74
|
+
end
|
75
|
+
|
76
|
+
def handle_s3_index_read(name, config, force:)
|
77
|
+
if force
|
78
|
+
var_name = "@_#{name}".to_sym
|
79
|
+
instance_variable_set(var_name, nil)
|
80
|
+
end
|
81
|
+
instance_var_set_if_needed(name) do
|
82
|
+
unless File.file?(full_cache_path(config))
|
83
|
+
index = handle_s3_index_record(name, config)
|
84
|
+
index.download!(s3: config[:s3_resource])
|
85
|
+
end
|
86
|
+
File.read(full_cache_path(config))
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def handle_s3_index_record(name, config)
|
91
|
+
instance_var_set_if_needed("#{name}_index") do
|
92
|
+
Index.find_by(origin_url: full_cache_path(config), s3_bucket: config[:bucket])
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def handle_s3_index_after_save(name, config, row)
|
97
|
+
value = instance_variable_get("@_#{name}")
|
98
|
+
handle_s3_index_write(name, config, value)
|
99
|
+
S3Index.upload!(
|
100
|
+
s3: config[:s3_resource],
|
101
|
+
bucket: config[:bucket],
|
102
|
+
src: full_cache_path(config)
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
def full_cache_path(config)
|
107
|
+
File.join(config[:cache_dir], "#{config[:name]}-#{self.id}")
|
108
|
+
end
|
109
|
+
|
110
|
+
def instance_var_set_if_needed(name, &block)
|
111
|
+
var_name = "@_#{name}".to_sym
|
112
|
+
result = instance_variable_get(var_name)
|
113
|
+
unless result
|
114
|
+
result = block.call
|
115
|
+
instance_variable_set(var_name, result)
|
116
|
+
end
|
117
|
+
result
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/lib/s3_index/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3_index
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Pierce
|
@@ -157,6 +157,7 @@ files:
|
|
157
157
|
- db/schema.rb
|
158
158
|
- lib/environment.rb
|
159
159
|
- lib/s3_index.rb
|
160
|
+
- lib/s3_index/accessor.rb
|
160
161
|
- lib/s3_index/index.rb
|
161
162
|
- lib/s3_index/version.rb
|
162
163
|
- lib/tasks/db.rake
|