tori 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63eb309ef18d6c682fec8fe879cba915aaa7080c
4
+ data.tar.gz: 4481a5b9462416c24e7da0cf766df0c8ba081a20
5
+ SHA512:
6
+ metadata.gz: cf11628e41240b07be7c7e4cdd7b4fe591eb0d939981fa713c59a5d1f688736657c144e037b3f94a433e249e5a98e6a4554a488b5b0293f2daf91d28379d6b93
7
+ data.tar.gz: aee79da9edd01954b4ab876fc18a7f8658f975db978f15881217ad0799249ee3c7bb8a0cbab9e48eeebe6c26b86080d926e7d9034d5a0d36fbfa7d93c71e0c5d
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 ksss
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ Tori
2
+ ===
3
+
4
+ Tori is a very very simple file uploader.
5
+
6
+ Tori dose nothing.
7
+
8
+ Only file upload to backend store.
9
+
10
+ You can upload file without alter database.
11
+
12
+ # Quick start on Rails
13
+
14
+ Gemfile
15
+
16
+ ```
17
+ gem 'tori', require: 'tori/rails'
18
+ ```
19
+
20
+ app/models/photo.rb
21
+
22
+ ```ruby
23
+ class Photo < ActiveRecord::Base
24
+ tori :image
25
+ end
26
+ ```
27
+
28
+ app/controllers/photos_controller.rb
29
+
30
+ ```ruby
31
+ class PhotosController < ApplicationController
32
+ def new
33
+ @photo = Photo.new
34
+ end
35
+
36
+ def create
37
+ Photo.create(photo_params)
38
+ redirect_to root_path
39
+ end
40
+
41
+ private
42
+
43
+ def photo_params
44
+ params.require(:photo).permit(:image)
45
+ end
46
+ end
47
+ ```
48
+
49
+ app/views/photos/new.html.slim
50
+
51
+ ```ruby
52
+ = form_for @photo, multipart: true |f|
53
+ = f.file_field 'image'
54
+ = f.button 'Upload'
55
+ ```
56
+
57
+ # default configure
58
+
59
+ ```
60
+ # Tori using hash function for decide filename.
61
+ # Filename dependent on class name and `id` setting with `tori` method in class.
62
+ Tori.config.hash_method = Digest::MD5.method(:hexdigest)
63
+
64
+ # Rails
65
+ Tori.config.backend = Tori::Backend::FileSystem.new(Rails.root.join('tmp', 'tori'))
66
+
67
+ # Other
68
+ Tori.config.backend = Tori::Backend::FileSystem.new(Pathname("tmp/tori"))
69
+ ```
70
+
71
+ You can change configure any time.
72
+
73
+ # Options
74
+
75
+ Change hash resource data.
76
+
77
+ ```
78
+ class Photo < ActiveRecord::Base
79
+ tori :image, id: :filename
80
+ def filename
81
+ "abc"
82
+ end
83
+ end
84
+ ```
85
+
86
+ This class never upload two file.
87
+
88
+ # future TODO
89
+
90
+ - support background S3 Storage
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ task :default => [:test]
4
+ Rake::TestTask.new {|t| t.libs << 'test'}
@@ -0,0 +1,18 @@
1
+ require 'tori/backend/filesystem'
2
+ require 'tori/config'
3
+ require 'tori/define'
4
+ require 'tori/version'
5
+ require 'pathname'
6
+ require 'digest/md5'
7
+ require "fileutils"
8
+
9
+ module Tori
10
+ class << self
11
+ def config
12
+ @config ||= Config.new.tap do |config|
13
+ config.backend = Tori::Backend::FileSystem.new(Pathname("tmp/tori"))
14
+ config.hash_method = Digest::MD5.method(:hexdigest)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Tori
2
+ module Backend
3
+ class FileSystem
4
+ def initialize(root)
5
+ @root = root
6
+ FileUtils.mkdir_p(@root.to_s)
7
+ end
8
+
9
+ def copy(uploader, filename)
10
+ IO.copy_stream(uploader, @root.join(filename))
11
+ end
12
+
13
+ def delete(filename)
14
+ File.unlink @root.join(filename)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Tori
2
+ class Config
3
+ attr_accessor :backend, :hash_method
4
+ def initialize
5
+ @backend = nil
6
+ @hash_method = nil
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module Tori
2
+ module Define
3
+ def tori(name, id: :id)
4
+ name_hash_get = "#{name}_hash".to_sym
5
+ name_ivar = "@#{name}".to_sym
6
+ name_hash_ivar = "@#{name}_hash".to_sym
7
+
8
+ define_method(name) do
9
+ instance_variable_get name_ivar
10
+ end
11
+
12
+ define_method("#{name}=") do |uploader|
13
+ instance_variable_set name_ivar, uploader
14
+ end
15
+
16
+ define_method(name_hash_get) do
17
+ Tori.config.hash_method.call "#{self.class.name}/#{__send__(id.to_sym)}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ require 'tori'
2
+
3
+ module Tori
4
+ class Engine < Rails::Engine
5
+ initializer "tori.setup", before: :load_environment_config do
6
+ # Default backend config
7
+ # You can change setting any time.
8
+ # Recommend to create config/initializer/tori.rb for setting.
9
+
10
+ # Configure for file store backend instance.
11
+ Tori.config.backend = Tori::Backend::FileSystem.new(Rails.root.join('tmp', 'tori'))
12
+
13
+ # Filename hashing method
14
+ # It's call when decide filename hash.
15
+ # `hash_method` must be have `call` method.
16
+ # default: `Digest::MD5.method(:hexdigest)``
17
+ # Tori.config.hash_method = Digest::MD5.method(:hexdigest)
18
+ end
19
+ end
20
+
21
+ module ActiveRecord
22
+ include Define
23
+
24
+ # Filename hash usgin `id` attribute by default.
25
+ # And you can change this attribute, But it's should be record unique.
26
+ #
27
+ # @example:
28
+ # class Photo < ActiveRecord::Base
29
+ # tori :image, id: :id
30
+ # end
31
+ def tori(name, id: :id)
32
+ super
33
+
34
+ name_hash_get = "#{name}_hash".to_sym
35
+
36
+ after_save do
37
+ uploader = __send__ name
38
+ filename = __send__ name_hash_get
39
+ Tori.config.backend.copy(uploader.path, filename) if uploader && filename
40
+ end
41
+
42
+ after_destroy do
43
+ filename = __send__ name_hash_get
44
+ Tori.config.backend.delete(filename) if filename
45
+ end
46
+ end
47
+ end
48
+ end
49
+ ::ActiveRecord::Base.extend(Tori::ActiveRecord)
@@ -0,0 +1,3 @@
1
+ module Tori
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'tori'
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class TestTori < Test::Unit::TestCase
4
+ test "config instance of" do
5
+ assert_instance_of Tori::Config, Tori.config
6
+ end
7
+
8
+ test "config default" do
9
+ assert_instance_of Tori::Backend::FileSystem, Tori.config.backend
10
+ assert { Digest::MD5.method(:hexdigest) == Tori.config.hash_method }
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class TestToriBackendFileSystem < Test::Unit::TestCase
4
+ class Uploader
5
+ def path
6
+ "/tmp/tori/test"
7
+ end
8
+ end
9
+
10
+ test "#initialize" do
11
+ path = Pathname("tmp/tori/test")
12
+ i = Tori::Backend::FileSystem.new(path)
13
+
14
+ assert_instance_of Tori::Backend::FileSystem, i
15
+ assert_raise(ArgumentError){ Tori::Backend::FileSystem.new }
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'test_helper'
2
+
3
+ class TestToriConfig < Test::Unit::TestCase
4
+ test "#initialize" do
5
+ i = Tori::Config.new
6
+ assert_instance_of Tori::Config, i
7
+ assert_respond_to i, :backend
8
+ assert_respond_to i, :backend=
9
+ assert_respond_to i, :hash_method
10
+ assert_respond_to i, :hash_method=
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ class TestToriDefine < Test::Unit::TestCase
4
+ class Dammy
5
+ extend Tori::Define
6
+ tori :test_image
7
+ def id
8
+ "dammy"
9
+ end
10
+ end
11
+
12
+ test "#tori" do
13
+ assert_respond_to Dammy.new, :test_image
14
+ assert_respond_to Dammy.new, :test_image=
15
+ assert_respond_to Dammy.new, :test_image_hash
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "tori/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tori"
7
+ spec.version = Tori::VERSION
8
+ spec.authors = ["ksss"]
9
+ spec.email = ["co000ri@gmail.com"]
10
+ spec.summary = %q{Simple file uploader}
11
+ spec.description = %q{Simple file uploader}
12
+ spec.homepage = "https://github.com/ksss/tori"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency 'test-unit'
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tori
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ksss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple file uploader
56
+ email:
57
+ - co000ri@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/tori.rb
68
+ - lib/tori/backend/filesystem.rb
69
+ - lib/tori/config.rb
70
+ - lib/tori/define.rb
71
+ - lib/tori/rails.rb
72
+ - lib/tori/version.rb
73
+ - test/test_helper.rb
74
+ - test/test_tori.rb
75
+ - test/test_tori_backend_filesystem.rb
76
+ - test/test_tori_config.rb
77
+ - test/test_tori_method_definition.rb
78
+ - tori.gemspec
79
+ homepage: https://github.com/ksss/tori
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.4.5
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Simple file uploader
103
+ test_files:
104
+ - test/test_helper.rb
105
+ - test/test_tori.rb
106
+ - test/test_tori_backend_filesystem.rb
107
+ - test/test_tori_config.rb
108
+ - test/test_tori_method_definition.rb