tori 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 804032544a779fc9014553a31b0ea5c2314a3db1
4
- data.tar.gz: 487ba2e0af21edf8bf41b4163301f66049d27768
3
+ metadata.gz: da06eed7aefecd6c1a93ecc13aa940c11ebdc1b6
4
+ data.tar.gz: 095deaa57d6d8ac0f6f91fde433dcb8f37112443
5
5
  SHA512:
6
- metadata.gz: 7027bb399d67358423ce0feb6655843f27381773afa1f500f1e35f1cec75fc8f2435b3e211f591e98f50bed23b144dbb31718444321c17f81cb0a64f9f266361
7
- data.tar.gz: 2d47426d4c1afb1b07458ff8ed442031995fda718ba1659723cc6ed630ce64b1a7adfc61fa56f94fae3ee261c265b126241a0578cb9dbb7c7efa7a1aa74eabff
6
+ metadata.gz: ba156535d1288ff070d8211ae5caf6d8cd0760fecf3382ed2e49f3839865d9341ab913693b3d2c066a8a1f036b6f81bfc185efc45ce27a1dba03348b82760882
7
+ data.tar.gz: f23d6e28c92d609875dc3fa99d36cacd5edc9bde2254b4e0b9d00a225ba92abe6ff7bc9391f52e9f06ebe55f5eecc6533d807a22e451963401c55b5315d439f5
data/README.md CHANGED
@@ -118,6 +118,7 @@ class PhotoController < ApplicationController
118
118
  photo.striped_image.write
119
119
  }
120
120
  end
121
+ end
121
122
  ```
122
123
 
123
124
  # Custom configure example
@@ -0,0 +1,52 @@
1
+ module Tori
2
+ module Backend
3
+ # Chain based on `exist?` method
4
+ # @example
5
+ # class Book < ActiveRecord::Base
6
+ # include Tori::Backend # short cut
7
+ #
8
+ # # If exist "lib/pdf" load this,
9
+ # # But nothing, Load from S3 "book" bucket.
10
+ # chain_backend = Chain.new(
11
+ # FileSystem.new(Pathname("lib/pdf")),
12
+ # S3.new(bucket: "book"),
13
+ # )
14
+ # tori :pdf, chain_backend do |model|
15
+ # "book/#{__tori__}/#{model.id}"
16
+ # end
17
+ # end
18
+ class Chain
19
+ class ExistError < StandardError
20
+ end
21
+
22
+ def initialize(*backends)
23
+ @backends = backends
24
+ end
25
+
26
+ def backend(filename)
27
+ @backends.each do |b|
28
+ if b.exist?(filename)
29
+ return b
30
+ end
31
+ end
32
+ raise ExistError, "exist(#{filename}) backend not found"
33
+ end
34
+
35
+ def exist?(filename)
36
+ backend(filename)
37
+ rescue ExistError
38
+ false
39
+ else
40
+ true
41
+ end
42
+
43
+ def read(filename)
44
+ backend(filename).read(filename)
45
+ end
46
+
47
+ def open(filename, &block)
48
+ backend(filename).open(filename, &block)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -39,6 +39,10 @@ module Tori
39
39
  ::File.read(path(filename), mode: 'rb')
40
40
  end
41
41
 
42
+ def open(filename, *rest, &block)
43
+ ::File.open(path(filename), *rest, &block)
44
+ end
45
+
42
46
  def path(filename)
43
47
  @root.join filename.to_s
44
48
  end
@@ -130,6 +130,26 @@ module Tori
130
130
  signer.presigned_url(method, bucket: @bucket, key: filename)
131
131
  end
132
132
 
133
+ def open(filename)
134
+ ext = ::File.extname(filename)
135
+ body = body(filename)
136
+
137
+ if block_given?
138
+ Tempfile.create([filename, ext]) do |f|
139
+ f.write body
140
+ f.fsync
141
+ f.rewind
142
+ yield f
143
+ end
144
+ else
145
+ f = Tempfile.open([filename, ext])
146
+ f.write body
147
+ f.fsync
148
+ f.rewind
149
+ f
150
+ end
151
+ end
152
+
133
153
  def get_object(opts={})
134
154
  client.get_object bucket: @bucket, **opts
135
155
  end
data/lib/tori/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tori
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/tori.rb CHANGED
@@ -1,9 +1,3 @@
1
- require 'tori/backend/filesystem'
2
- require 'tori/config'
3
- require 'tori/context'
4
- require 'tori/define'
5
- require 'tori/file'
6
- require 'tori/version'
7
1
  require 'pathname'
8
2
  require 'digest/sha1'
9
3
  require "fileutils"
@@ -34,3 +28,11 @@ module Tori
34
28
  end
35
29
  end
36
30
  end
31
+
32
+ require 'tori/backend/filesystem'
33
+ require 'tori/backend/chain'
34
+ require 'tori/config'
35
+ require 'tori/context'
36
+ require 'tori/define'
37
+ require 'tori/file'
38
+ require 'tori/version'
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ class TestToriBackendChain < Test::Unit::TestCase
4
+ include Tori::Backend
5
+
6
+ setup do
7
+ @filesystem1 = FileSystem.new(Pathname("test/tmp/tori/store1"))
8
+ File.open(@filesystem1.root.join("testfile-1"), 'w+'){ |f| f.write("text-1") }
9
+
10
+ @filesystem2 = FileSystem.new(Pathname("test/tmp/tori/store2"))
11
+ File.open(@filesystem2.root.join("testfile-2"), 'w+'){ |f| f.write("text-2") }
12
+
13
+ @backend = Chain.new(@filesystem1, @filesystem2)
14
+ end
15
+
16
+ test "#backend" do
17
+ assert { @filesystem1 == @backend.backend("testfile-1") }
18
+ assert { @filesystem2 == @backend.backend("testfile-2") }
19
+ assert_raise(Chain::ExistError) { @backend.backend("testfile-3") }
20
+ end
21
+
22
+ test "#exist?" do
23
+ assert { true == @backend.exist?("testfile-1") }
24
+ assert { true == @backend.exist?("testfile-2") }
25
+ assert { false == @backend.exist?("testfile-3") }
26
+ end
27
+
28
+ test "read" do
29
+ assert { "text-1" == @backend.read("testfile-1") }
30
+ assert { "text-2" == @backend.read("testfile-2") }
31
+ assert_raise(Chain::ExistError) { @backend.read("testfile-3") }
32
+ end
33
+
34
+ test "open" do
35
+ @backend.open("testfile-1") do |f|
36
+ assert { "text-1" == f.read }
37
+ assert { "test/tmp/tori/store1/testfile-1" == f.path }
38
+ end
39
+ @backend.open("testfile-2") do |f|
40
+ assert { "text-2" == f.read }
41
+ assert { "test/tmp/tori/store2/testfile-2" == f.path }
42
+ end
43
+ assert_raise(Chain::ExistError) { @backend.read("testfile-3") }
44
+ end
45
+ end
@@ -18,6 +18,7 @@ class TestToriBackendFileSystem < Test::Unit::TestCase
18
18
 
19
19
  test "#exist?" do
20
20
  assert { true == @filesystem.exist?(".") }
21
+ assert { true == @filesystem.exist?("testfile") }
21
22
  assert { false == @filesystem.exist?("nothing_file") }
22
23
  end
23
24
 
@@ -55,4 +56,13 @@ class TestToriBackendFileSystem < Test::Unit::TestCase
55
56
  @filesystem.write(Pathname.new("copyfile"), "string")
56
57
  assert { "string" == @filesystem.read("copyfile") }
57
58
  end
59
+
60
+ test "#open" do
61
+ @filesystem.open("testfile") do |f|
62
+ assert_instance_of File, f
63
+ end
64
+ f = @filesystem.open("testfile")
65
+ assert_instance_of File, f
66
+ f.close
67
+ end
58
68
  end
@@ -111,6 +111,18 @@ class TestToriBackendS3 < Test::Unit::TestCase
111
111
  assert_match @backend.bucket, @backend.public_url("testfile")
112
112
  assert_match "testfile", @backend.public_url("testfile")
113
113
  end
114
+
115
+ test "#open" do
116
+ path = nil
117
+ @backend.open("testfile") do |f|
118
+ assert_instance_of File, f
119
+ path = f.path
120
+ end
121
+ assert { false == File.exist?(path) }
122
+ f = @backend.open("testfile")
123
+ assert_instance_of Tempfile, f
124
+ f.close!
125
+ end
114
126
  end
115
127
 
116
128
  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.5.0
4
+ version: 0.6.0
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-06 00:00:00.000000000 Z
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -94,6 +94,7 @@ files:
94
94
  - README.md
95
95
  - Rakefile
96
96
  - lib/tori.rb
97
+ - lib/tori/backend/chain.rb
97
98
  - lib/tori/backend/filesystem.rb
98
99
  - lib/tori/backend/s3.rb
99
100
  - lib/tori/config.rb
@@ -104,6 +105,7 @@ files:
104
105
  - lib/tori/version.rb
105
106
  - test/test_helper.rb
106
107
  - test/test_tori.rb
108
+ - test/test_tori_backend_chain.rb
107
109
  - test/test_tori_backend_filesystem.rb
108
110
  - test/test_tori_backend_s3.rb
109
111
  - test/test_tori_config.rb
@@ -137,6 +139,7 @@ summary: Simple file uploader
137
139
  test_files:
138
140
  - test/test_helper.rb
139
141
  - test/test_tori.rb
142
+ - test/test_tori_backend_chain.rb
140
143
  - test/test_tori_backend_filesystem.rb
141
144
  - test/test_tori_backend_s3.rb
142
145
  - test/test_tori_config.rb