tori 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/lib/tori/backend/filesystem.rb +10 -5
- data/lib/tori/backend/s3.rb +47 -0
- data/lib/tori/config.rb +9 -1
- data/lib/tori/define.rb +5 -5
- data/lib/tori/file.rb +22 -11
- data/lib/tori/rails.rb +2 -2
- data/lib/tori/version.rb +1 -1
- data/lib/tori.rb +7 -2
- data/test/test_tori_config.rb +0 -4
- data/test/{test_tori_method_definition.rb → test_tori_define.rb} +8 -0
- data/test/test_tori_file.rb +24 -12
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b89b585791768dfdcae240e4f4901c8f047983e
|
4
|
+
data.tar.gz: d7dd7fae6890630c058c31a80d8318bd7fe66b3f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af4ec806666e726efcd93367ad5a566621d7018eebce285f74d37c0807371df9b9301e78f8c30bd477307c0ce0c11998eaa345cba13e7f290d06692b4a596d90
|
7
|
+
data.tar.gz: c7864d0df3928c94ff43df756c822df1c424d4b76effbc68ef73d688c4ccd946c9ce4f1b4d2041c054f2291f2addab37e2a024c03c2df95dc0b0485cdad21027
|
data/README.md
CHANGED
@@ -56,7 +56,28 @@ app/views/photos/new.html.slim
|
|
56
56
|
= f.button 'Upload'
|
57
57
|
```
|
58
58
|
|
59
|
-
|
59
|
+
You can read file.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
photo.image.read #=> image bin
|
63
|
+
photo.image.exist? #=> exist check
|
64
|
+
photo.image.to_s #=> filename
|
65
|
+
```
|
66
|
+
|
67
|
+
# Custom configure example
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
# Save to S3 bucket.
|
71
|
+
require 'tori/backend/s3'
|
72
|
+
Tori.config.backend = Tori::Backend::S3.new('tori_bucket')
|
73
|
+
|
74
|
+
# Filename decided by model.class.name,id and hidden words.
|
75
|
+
Tori.config.filename_callback do |model|
|
76
|
+
"#{model.class.name}/#{Digest::SHA1.hexdigest "#{ENV["TORI_MAGICKWORD"]}/#{model.id}"}"
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
# Default configure
|
60
81
|
|
61
82
|
[https://github.com/ksss/tori/blob/master/lib/tori.rb](https://github.com/ksss/tori/blob/master/lib/tori.rb)
|
62
83
|
|
@@ -4,23 +4,28 @@ module Tori
|
|
4
4
|
attr_accessor :root
|
5
5
|
def initialize(root)
|
6
6
|
@root = root
|
7
|
-
FileUtils.mkdir_p
|
7
|
+
FileUtils.mkdir_p @root.to_s
|
8
8
|
end
|
9
9
|
|
10
10
|
def copy(form_path, to_filename)
|
11
|
-
IO.copy_stream
|
11
|
+
IO.copy_stream form_path.to_s, path(to_filename)
|
12
12
|
end
|
13
13
|
|
14
14
|
def delete(filename)
|
15
|
-
::File.unlink
|
15
|
+
::File.unlink path(filename)
|
16
16
|
end
|
17
17
|
|
18
18
|
def exist?(filename)
|
19
|
-
::File.exist?
|
19
|
+
::File.exist? path(filename)
|
20
20
|
end
|
21
|
+
alias exists? exist?
|
21
22
|
|
22
23
|
def read(filename)
|
23
|
-
::File.read
|
24
|
+
::File.read path(filename)
|
25
|
+
end
|
26
|
+
|
27
|
+
def path(filename)
|
28
|
+
@root.join filename.to_s
|
24
29
|
end
|
25
30
|
end
|
26
31
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'aws-sdk'
|
2
|
+
|
3
|
+
module Tori
|
4
|
+
module Backend
|
5
|
+
class S3
|
6
|
+
# You must be set bucket name.
|
7
|
+
# And you can configurate to S3
|
8
|
+
# But, you can also configurate by AWS.config()
|
9
|
+
#
|
10
|
+
# example:
|
11
|
+
# Tori.config.backend = Tori::Backend::S3.new(
|
12
|
+
# bucket: 'photos',
|
13
|
+
# region: '...',
|
14
|
+
# s3_encryption_key: '...'
|
15
|
+
# )
|
16
|
+
def initialize(bucket:, **s3_config)
|
17
|
+
s3 = AWS::S3.new(s3_config)
|
18
|
+
@bucket = s3.buckets[bucket]
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy(form_path, filename)
|
22
|
+
object(filename).write(file: form_path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete(filename)
|
26
|
+
object(filename).delete
|
27
|
+
end
|
28
|
+
|
29
|
+
def exist?(filename)
|
30
|
+
object(filename).exists?
|
31
|
+
end
|
32
|
+
alias exists? exist?
|
33
|
+
|
34
|
+
def read(filename)
|
35
|
+
object(filename).read
|
36
|
+
end
|
37
|
+
|
38
|
+
def url(filename)
|
39
|
+
object(filename).url_for(:read)
|
40
|
+
end
|
41
|
+
|
42
|
+
def object(filename)
|
43
|
+
@bucket.objects[filename]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/tori/config.rb
CHANGED
@@ -1,9 +1,17 @@
|
|
1
1
|
module Tori
|
2
2
|
class Config
|
3
|
-
attr_accessor :backend
|
3
|
+
attr_accessor :backend
|
4
4
|
def initialize
|
5
5
|
@backend = nil
|
6
6
|
@filename_callback = nil
|
7
7
|
end
|
8
|
+
|
9
|
+
def filename_callback(&block)
|
10
|
+
if block_given?
|
11
|
+
@filename_callback = block
|
12
|
+
else
|
13
|
+
@filename_callback
|
14
|
+
end
|
15
|
+
end
|
8
16
|
end
|
9
17
|
end
|
data/lib/tori/define.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
module Tori
|
2
2
|
module Define
|
3
3
|
def tori(name)
|
4
|
-
|
4
|
+
name_ivar = "@#{name}".to_sym
|
5
5
|
|
6
6
|
define_method(name) do
|
7
|
-
ivar = instance_variable_get
|
8
|
-
instance_variable_set
|
7
|
+
ivar = instance_variable_get name_ivar
|
8
|
+
instance_variable_set name_ivar, ivar || File.new(self)
|
9
9
|
end
|
10
10
|
|
11
11
|
define_method("#{name}=") do |uploader|
|
12
|
-
file = File.new(self, uploader)
|
13
|
-
instance_variable_set
|
12
|
+
file = File.new(self, from: uploader)
|
13
|
+
instance_variable_set name_ivar, file
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
data/lib/tori/file.rb
CHANGED
@@ -1,28 +1,39 @@
|
|
1
1
|
module Tori
|
2
2
|
class File
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(model, from = nil)
|
3
|
+
def initialize(model, from: nil)
|
6
4
|
@model = model
|
7
5
|
@from = from
|
8
6
|
end
|
9
7
|
|
10
|
-
def
|
8
|
+
def name
|
11
9
|
Tori.config.filename_callback.call(@model)
|
12
10
|
end
|
13
|
-
|
14
|
-
def exist?
|
15
|
-
Tori.config.backend.exist?(to_s)
|
16
|
-
end
|
11
|
+
alias to_s name
|
17
12
|
|
18
13
|
def copy?
|
19
|
-
!@model.nil? && !@from.nil? && @from.respond_to?(:path) && 0 <
|
14
|
+
!@model.nil? && !@from.nil? && @from.respond_to?(:path) && 0 < name.length
|
20
15
|
rescue NameError => e
|
21
16
|
false
|
22
17
|
end
|
23
18
|
|
24
|
-
def
|
25
|
-
Tori.config.backend.
|
19
|
+
def copy
|
20
|
+
Tori.config.backend.copy @from.path, name if copy?
|
21
|
+
end
|
22
|
+
|
23
|
+
def delete
|
24
|
+
Tori.config.backend.delete name if exist?
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_to_missing?(sym, include_private)
|
28
|
+
Tori.config.backend.respond_to?(sym, include_private)
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(sym)
|
32
|
+
if respond_to_missing?(sym, false)
|
33
|
+
Tori.config.backend.__send__ sym, name
|
34
|
+
else
|
35
|
+
fail NameError, "undefined local variable or method `#{sym}' for #{Tori.config.backend.inspect}"
|
36
|
+
end
|
26
37
|
end
|
27
38
|
end
|
28
39
|
end
|
data/lib/tori/rails.rb
CHANGED
@@ -16,12 +16,12 @@ module Tori
|
|
16
16
|
|
17
17
|
after_save do
|
18
18
|
file = __send__ name
|
19
|
-
|
19
|
+
file.copy
|
20
20
|
end
|
21
21
|
|
22
22
|
after_destroy do
|
23
23
|
file = __send__ name
|
24
|
-
|
24
|
+
file.delete
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/tori/version.rb
CHANGED
data/lib/tori.rb
CHANGED
@@ -18,10 +18,15 @@ module Tori
|
|
18
18
|
# Configure for file store backend instance.
|
19
19
|
config.backend = Tori::Backend::FileSystem.new(Pathname("tmp/tori"))
|
20
20
|
|
21
|
+
# You can also use S3 backend.
|
22
|
+
# It take 'aws-sdk' gem.
|
23
|
+
# S3 example
|
24
|
+
# require 'tori/backend/s3'
|
25
|
+
# config.backend = Tori::Backend::S3.new('tori_bucket')
|
26
|
+
|
21
27
|
# Filename hashing method
|
22
28
|
# It's call when decide filename hash.
|
23
|
-
|
24
|
-
config.filename_callback = ->(model) do
|
29
|
+
config.filename_callback do |model|
|
25
30
|
Digest::SHA1.hexdigest "#{model.class.name}/#{model.id}"
|
26
31
|
end
|
27
32
|
end
|
data/test/test_tori_config.rb
CHANGED
@@ -4,9 +4,5 @@ class TestToriConfig < Test::Unit::TestCase
|
|
4
4
|
test "#initialize" do
|
5
5
|
i = Tori::Config.new
|
6
6
|
assert_instance_of Tori::Config, i
|
7
|
-
assert_respond_to i, :backend
|
8
|
-
assert_respond_to i, :backend=
|
9
|
-
assert_respond_to i, :filename_callback
|
10
|
-
assert_respond_to i, :filename_callback=
|
11
7
|
end
|
12
8
|
end
|
@@ -6,8 +6,16 @@ class TestToriDefine < Test::Unit::TestCase
|
|
6
6
|
tori :test_image
|
7
7
|
end
|
8
8
|
|
9
|
+
class Uploader
|
10
|
+
end
|
11
|
+
|
9
12
|
test "#tori" do
|
10
13
|
assert_respond_to Dammy.new, :test_image
|
11
14
|
assert_respond_to Dammy.new, :test_image=
|
12
15
|
end
|
16
|
+
|
17
|
+
test "defined methods" do
|
18
|
+
assert_instance_of Uploader, Dammy.new.test_image = Uploader.new
|
19
|
+
assert_instance_of Tori::File, Dammy.new.test_image
|
20
|
+
end
|
13
21
|
end
|
data/test/test_tori_file.rb
CHANGED
@@ -3,38 +3,50 @@ require 'test_helper'
|
|
3
3
|
class TestToriDefine < Test::Unit::TestCase
|
4
4
|
setup do
|
5
5
|
@orig = Tori.config.filename_callback
|
6
|
-
Tori.config.filename_callback
|
6
|
+
Tori.config.filename_callback do |model|
|
7
7
|
model
|
8
|
-
|
8
|
+
end
|
9
9
|
Tori.config.backend = Tori::Backend::FileSystem.new Pathname("test").join("tmp")
|
10
10
|
end
|
11
11
|
|
12
12
|
teardown do
|
13
|
-
Tori.config.filename_callback
|
13
|
+
Tori.config.filename_callback &@orig
|
14
14
|
FileUtils.rm_rf("test/tmp")
|
15
15
|
end
|
16
16
|
|
17
|
-
class
|
17
|
+
class From
|
18
18
|
def path
|
19
|
-
|
19
|
+
__FILE__
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
test "#initialize" do
|
24
|
-
assert_instance_of Tori::File, Tori::File.new(nil
|
24
|
+
assert_instance_of Tori::File, Tori::File.new(nil)
|
25
|
+
assert_instance_of Tori::File, Tori::File.new(nil, from: nil)
|
25
26
|
end
|
26
27
|
|
27
|
-
test "#
|
28
|
-
assert { "test" == Tori::File.new("test"
|
28
|
+
test "#name" do
|
29
|
+
assert { "test" == Tori::File.new("test").name }
|
29
30
|
end
|
30
31
|
|
31
32
|
test "#exist?" do
|
32
|
-
assert { true == Tori::File.new(__FILE__
|
33
|
-
assert { false == Tori::File.new("nothing_file"
|
33
|
+
assert { true == Tori::File.new(__FILE__).exist? }
|
34
|
+
assert { false == Tori::File.new("nothing_file").exist? }
|
34
35
|
end
|
35
36
|
|
36
37
|
test "#copy?" do
|
37
|
-
assert { false == Tori::File.new(__FILE__
|
38
|
-
assert { true == Tori::File.new(__FILE__,
|
38
|
+
assert { false == Tori::File.new(__FILE__).copy? }
|
39
|
+
assert { true == Tori::File.new(__FILE__, from: From.new).copy? }
|
40
|
+
end
|
41
|
+
|
42
|
+
test "copy" do
|
43
|
+
assert { false == File.exist?("test/tmp/copy") }
|
44
|
+
Tori::File.new("copy", from: From.new).copy
|
45
|
+
assert { true == File.exist?("test/tmp/copy") }
|
46
|
+
end
|
47
|
+
|
48
|
+
test "#method_missing" do
|
49
|
+
assert { true == Tori::File.new(nil).respond_to?(:read) }
|
50
|
+
assert_raise(NameError) { Tori::File.new(nil).undefined }
|
39
51
|
end
|
40
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tori
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ksss
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- Rakefile
|
67
67
|
- lib/tori.rb
|
68
68
|
- lib/tori/backend/filesystem.rb
|
69
|
+
- lib/tori/backend/s3.rb
|
69
70
|
- lib/tori/config.rb
|
70
71
|
- lib/tori/define.rb
|
71
72
|
- lib/tori/file.rb
|
@@ -75,8 +76,8 @@ files:
|
|
75
76
|
- test/test_tori.rb
|
76
77
|
- test/test_tori_backend_filesystem.rb
|
77
78
|
- test/test_tori_config.rb
|
79
|
+
- test/test_tori_define.rb
|
78
80
|
- test/test_tori_file.rb
|
79
|
-
- test/test_tori_method_definition.rb
|
80
81
|
- tori.gemspec
|
81
82
|
homepage: https://github.com/ksss/tori
|
82
83
|
licenses:
|
@@ -107,5 +108,5 @@ test_files:
|
|
107
108
|
- test/test_tori.rb
|
108
109
|
- test/test_tori_backend_filesystem.rb
|
109
110
|
- test/test_tori_config.rb
|
111
|
+
- test/test_tori_define.rb
|
110
112
|
- test/test_tori_file.rb
|
111
|
-
- test/test_tori_method_definition.rb
|