tori 0.3.0 → 0.4.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: 7f9e73b59d43f6d816af38e163263f8ad366b29f
4
- data.tar.gz: 61c1bfa6790ebf0b8f5e9ee6aa967c2a56d4150f
3
+ metadata.gz: 008108fbf732144ba0e7b7d840daf1b5ceeaf776
4
+ data.tar.gz: ab5268fcb9f9e216eef8347d5b0b32f2684f6a82
5
5
  SHA512:
6
- metadata.gz: 62cf0872b2c0798d92a223c45badb6a7665bbddf923af5b42ef3e24588a081632b4e26805cbfde96092899c3fb8ea4730288a6e28fb68dccfd66c4d7c90f17e9
7
- data.tar.gz: c95fe43b976782cc20085f1ef00a562bd3b3eba620d6c13ecc66c6c19909dcbb1680f4962f4b1444dbbb0d26e930aae77211ce3944be51ff97e84d2f1142392b
6
+ metadata.gz: 25ddda231a1ee85f8353d65f214a5ae6fbb0f6cf547615c907cd05caf4dbb47b9263ac77550496e7c0d9fe6c4b96f5c5548e0a548fef375503ab6db7eb53f781
7
+ data.tar.gz: ab35a2002c3203fdfa3dd5ca32e93c7dbdd3d8238dcefccadb9a540f33865274bf2b7a7a74e1a4420ff07dbcb98f572363b9d79212949caf508121fa0bed7d61
data/README.md CHANGED
@@ -115,7 +115,6 @@ class PhotoController < ApplicationController
115
115
  end
116
116
  ```
117
117
 
118
-
119
118
  # Custom configure example
120
119
 
121
120
  ```ruby
@@ -127,6 +126,16 @@ Tori.config.backend = Tori::Backend::S3.new(bucket: 'tori_bucket')
127
126
  Tori.config.filename_callback do |model|
128
127
  "#{model.class.name}/#{Digest::SHA1.hexdigest "#{ENV["TORI_MAGICKWORD"]}/#{model.id}"}"
129
128
  end
129
+
130
+ # You can change any way define filename.
131
+ Tori.config.filename_callback do |model|
132
+ # `__tori__` method is meta name of defined by `tori` method.
133
+ "#{model.class.name}/#{__tori__}/#{model.title}"
134
+ # class Photo
135
+ # tori :orig #=> "Photo/orig/cool-shot.png"
136
+ # tori :strip #=> "Photo/strip/cool-shot.png"
137
+ # end
138
+ end
130
139
  ```
131
140
 
132
141
  # Default configure
@@ -97,8 +97,8 @@ module Tori
97
97
  )[:body]
98
98
  end
99
99
 
100
- def put(filename, body)
101
- put_object key: filename, body: body
100
+ def put(filename, body, opts={})
101
+ put_object key: filename, body: body, **opts
102
102
  end
103
103
 
104
104
  def head(filename = nil)
@@ -118,24 +118,24 @@ module Tori
118
118
  signer.presigned_url(method, bucket: @bucket, key: filename)
119
119
  end
120
120
 
121
- def get_object(key:)
122
- client.get_object bucket: @bucket, key: key
121
+ def get_object(opts={})
122
+ client.get_object bucket: @bucket, **opts
123
123
  end
124
124
 
125
- def head_object(key:)
126
- client.head_object bucket: @bucket, key: key
125
+ def head_object(opts={})
126
+ client.head_object bucket: @bucket, **opts
127
127
  end
128
128
 
129
- def head_bucket
130
- client.head_bucket bucket: @bucket
129
+ def head_bucket(opts={})
130
+ client.head_bucket bucket: @bucket, **opts
131
131
  end
132
132
 
133
133
  def put_object(opts = {})
134
- client.put_object({bucket: @bucket}.merge(opts))
134
+ client.put_object bucket: @bucket, **opts
135
135
  end
136
136
 
137
- def delete_object(key:)
138
- client.delete_object bucket: @bucket, key: key
137
+ def delete_object(opts={})
138
+ client.delete_object bucket: @bucket, **opts
139
139
  end
140
140
  end
141
141
  end
@@ -0,0 +1,11 @@
1
+ module Tori
2
+ class Context
3
+ def initialize(name)
4
+ @name = name
5
+ end
6
+
7
+ def __tori__
8
+ @name
9
+ end
10
+ end
11
+ end
data/lib/tori/define.rb CHANGED
@@ -5,11 +5,11 @@ module Tori
5
5
 
6
6
  define_method(name) do
7
7
  ivar = instance_variable_get name_ivar
8
- ivar || instance_variable_set(name_ivar, File.new(self, &block))
8
+ ivar || instance_variable_set(name_ivar, File.new(self, title: name, &block))
9
9
  end
10
10
 
11
11
  define_method("#{name}=") do |uploader|
12
- file = File.new(self, from: uploader, &block)
12
+ file = File.new(self, title: name, from: uploader, &block)
13
13
  instance_variable_set name_ivar, file
14
14
  end
15
15
  end
data/lib/tori/file.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  module Tori
2
2
  class File
3
- def initialize(model, from: nil, &block)
3
+ def initialize(model, title: nil, from: nil, &block)
4
4
  @model = model
5
+ @title = title
5
6
  if from.respond_to?(:read) and from.respond_to?(:rewind)
6
7
  from.rewind
7
8
  @from = from.read
@@ -12,10 +13,13 @@ module Tori
12
13
  end
13
14
 
14
15
  def name
16
+ context = Context.new(@title)
15
17
  if @filename_callback
16
- @filename_callback.call(@model)
18
+ context.define_singleton_method(:__bind__, @filename_callback)
19
+ context.__bind__(@model)
17
20
  else
18
- Tori.config.filename_callback.call(@model)
21
+ context.define_singleton_method(:__bind__, Tori.config.filename_callback)
22
+ context.__bind__(@model)
19
23
  end
20
24
  end
21
25
  alias to_s name
data/lib/tori/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tori
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/tori.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'tori/backend/filesystem'
2
2
  require 'tori/config'
3
+ require 'tori/context'
3
4
  require 'tori/define'
4
5
  require 'tori/file'
5
6
  require 'tori/version'
@@ -4,6 +4,10 @@ class TestToriDefine < Test::Unit::TestCase
4
4
  class Dammy
5
5
  extend Tori::Define
6
6
  tori :test_image
7
+ bird = "piyo"
8
+ tori :def_image do |model|
9
+ "foo/#{__tori__}/bar/#{bird}"
10
+ end
7
11
  end
8
12
 
9
13
  class Uploader
@@ -12,6 +16,8 @@ class TestToriDefine < Test::Unit::TestCase
12
16
  test "#tori" do
13
17
  assert_respond_to Dammy.new, :test_image
14
18
  assert_respond_to Dammy.new, :test_image=
19
+ assert_respond_to Dammy.new, :def_image
20
+ assert_respond_to Dammy.new, :def_image=
15
21
  end
16
22
 
17
23
  test "defined methods" do
@@ -20,4 +26,10 @@ class TestToriDefine < Test::Unit::TestCase
20
26
  assert_instance_of Uploader, dammy.test_image = Uploader.new
21
27
  assert_instance_of Tori::File, dammy.test_image
22
28
  end
29
+
30
+ test "define name" do
31
+ dammy = Dammy.new
32
+ assert_instance_of Tori::File, dammy.def_image
33
+ assert { "foo/def_image/bar/piyo" == dammy.def_image.name }
34
+ end
23
35
  end
@@ -26,7 +26,10 @@ class TestToriFile < Test::Unit::TestCase
26
26
  test "#initialize" do
27
27
  assert_instance_of Tori::File, Tori::File.new(nil)
28
28
  assert_instance_of Tori::File, Tori::File.new(nil, from: nil)
29
+ assert_instance_of Tori::File, Tori::File.new(nil, title: nil)
29
30
  assert_instance_of Tori::File, Tori::File.new(nil, from: nil) { }
31
+ assert_raise(ArgumentError) { Tori::File.new }
32
+ assert_raise(ArgumentError) { Tori::File.new(nil, nothing: nil) }
30
33
  end
31
34
 
32
35
  test "#name" do
@@ -65,6 +68,16 @@ class TestToriFile < Test::Unit::TestCase
65
68
  assert { "should be match ;)" == tori_file.read }
66
69
  end
67
70
 
71
+ test "with title" do
72
+ before = Tori.config.filename_callback
73
+ Tori.config.filename_callback do |model|
74
+ "#{model}/#{__tori__}"
75
+ end
76
+ assert { "test/" == Tori::File.new("test").name }
77
+ assert { "test/tori" == Tori::File.new("test", title: "tori").name }
78
+ Tori.config.filename_callback &before
79
+ end
80
+
68
81
  test "#method_missing" do
69
82
  assert { true == Tori::File.new(nil).respond_to?(:read) }
70
83
  assert_raise(NameError) { Tori::File.new(nil).undefined }
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.3.0
4
+ version: 0.4.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-09-23 00:00:00.000000000 Z
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -97,6 +97,7 @@ files:
97
97
  - lib/tori/backend/filesystem.rb
98
98
  - lib/tori/backend/s3.rb
99
99
  - lib/tori/config.rb
100
+ - lib/tori/context.rb
100
101
  - lib/tori/define.rb
101
102
  - lib/tori/file.rb
102
103
  - lib/tori/rails.rb
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  version: '0'
130
131
  requirements: []
131
132
  rubyforge_project:
132
- rubygems_version: 2.4.5.1
133
+ rubygems_version: 2.5.0
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: Simple file uploader