tori-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7504c47ec18bfbc0388eeb3aec3e92b6bd0d91d2
4
+ data.tar.gz: 88cfcb2893b4e2dd03b85507cd81406da61a8b5c
5
+ SHA512:
6
+ metadata.gz: e30d29807002e71f8eed6ba1fd49b787554366e390563ec72e63b901294e4df7752f67dccfb8d02d6897f79d61ccac299d90441044d328883415c8fd7b155dc2
7
+ data.tar.gz: b79fe45199dda07b98c93c1d54ed3cbb125ff25e7f23643173ed501036d62a65020f39bc77e451f42ca2412fdf1d67aa4b8ed2fc23ef674d4ee738cf4a768749
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tori-rails.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Tori::Rails
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tori/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tori-rails'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tori-rails
22
+
23
+ ## Usage
24
+
25
+ ``` ruby
26
+ class Image < ActiveRecord::Base
27
+ include Tori::Rails::Registerable
28
+ end
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tori-rails.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tori/rails"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/tori/rails.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "tori/rails/version"
2
+
3
+ module Tori
4
+ module Rails
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,79 @@
1
+ module Tori
2
+ module Rails
3
+ class Callback
4
+ def initialize
5
+ @callbacks = {
6
+ store_key: [],
7
+ before_upload: [],
8
+ upload: [],
9
+ after_upload: [],
10
+ destroy: []
11
+ }
12
+ end
13
+
14
+ def register(type, args)
15
+ @callbacks[type] << {
16
+ name: args[:name],
17
+ callback: args[:callback],
18
+ args: args.slice!(:name, :callback)
19
+ }
20
+ end
21
+
22
+ def before_validation(record)
23
+ @callbacks[:store_key].each do |callback|
24
+ store_dummy_key(record, key: callback[:args][:key])
25
+ end
26
+ end
27
+
28
+ def after_save(record)
29
+ @callbacks[:before_upload].each do |callback|
30
+ record.send(callback[:callback], name: callback[:name])
31
+ end
32
+ @callbacks[:upload].each do |callback|
33
+ upload_image(record, name: callback[:name])
34
+ end
35
+ @callbacks[:after_upload].each do |callback|
36
+ record.send(callback[:callback], name: callback[:name])
37
+ end
38
+ @callbacks[:store_key].each do |callback|
39
+ store_key(record, name: callback[:name], key: callback[:args][:key])
40
+ end
41
+ end
42
+
43
+ def after_destroy(record)
44
+ @callbacks[:destroy].each do |callback|
45
+ destroy_image(record, name: callback[:name])
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def content_type(record, name:)
52
+ FileMagic.open(FileMagic::MAGIC_MIME) do |fm|
53
+ mime = fm.buffer(record.send(name).from)
54
+ mime = 'image/svg+xml' if mime.split(';')[0] == 'application/xml' # application/xml は SVG と判断する
55
+ mime
56
+ end
57
+ end
58
+
59
+ def upload_image(record, name:)
60
+ record.send(name).write(content_type: content_type(record, name: name), acl: "public-read") if record.send(name).from?
61
+ end
62
+
63
+ def destroy_image(record, name:)
64
+ record.send(name).delete
65
+ end
66
+
67
+ def store_dummy_key(record, key:)
68
+ # for evade validation errors
69
+ record.send(:write_attribute, key, "dummy")
70
+ end
71
+
72
+ def store_key(record, name:, key:)
73
+ s3_key = record.send(name).name
74
+ fail ErrorStoredKeyMissing unless s3_key.present?
75
+ record.update_column(key, s3_key)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ module Tori
2
+ module Rails
3
+ module Define
4
+ extend Tori::Define
5
+
6
+ def tori(name, args, &block)
7
+ register_callbacks(name, args)
8
+
9
+ args.delete(:store_key)
10
+ args.delete(:before_upload)
11
+ args.delete(:skip_upload)
12
+ args.delete(:after_upload)
13
+ args.delete(:skip_destroy)
14
+
15
+ super(name, args, &block)
16
+ end
17
+
18
+ private
19
+
20
+ def register_callbacks(name, args)
21
+ Array(args[:store_key]).each do |store_key|
22
+ callback.register(:store_key, name: name, key: store_key)
23
+ end
24
+ Array(args[:before_upload]).each do |method|
25
+ callback.register(:before_upload, name: name, callback: method)
26
+ end
27
+ callback.register(:upload, name: name) unless args[:skip_upload]
28
+ Array(args[:after_upload]).each do |method|
29
+ callback.register(:after_upload, name: name, callback: method)
30
+ end
31
+ callback.register(:destroy, name: name) unless args[:skip_destroy]
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,8 @@
1
+ module Tori
2
+ module Rails
3
+ module Error
4
+ class StoredKeyMissing < StandardError; end
5
+ class StoredColumnMissing < StandardError; end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,22 @@
1
+ module Tori
2
+ module Rails
3
+ module Registerable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ extend Tori::Rails::Define
8
+ @tori_rails_callback = Tori::Rails::Callback.new
9
+
10
+ before_validation @tori_rails_callback
11
+ after_save @tori_rails_callback
12
+ after_destroy @tori_rails_callback
13
+ end
14
+
15
+ class_methods do
16
+ def callback
17
+ @tori_rails_callback
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Tori
2
+ module Rails
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tori/rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tori-rails"
8
+ spec.version = Tori::Rails::VERSION
9
+ spec.authors = ["asonas"]
10
+ spec.email = ["hzw1258@gmail.com"]
11
+
12
+ spec.summary = %q{tori with rails}
13
+ spec.description = %q{tori with rails}
14
+ spec.homepage = "https://github.com/asonas/tori-rails"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tori-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - asonas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-20 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: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: tori with rails
42
+ email:
43
+ - hzw1258@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - README.md
51
+ - Rakefile
52
+ - bin/console
53
+ - bin/setup
54
+ - lib/tori/rails.rb
55
+ - lib/tori/rails/callback.rb
56
+ - lib/tori/rails/define.rb
57
+ - lib/tori/rails/error.rb
58
+ - lib/tori/rails/registerable.rb
59
+ - lib/tori/rails/version.rb
60
+ - tori-rails.gemspec
61
+ homepage: https://github.com/asonas/tori-rails
62
+ licenses: []
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: tori with rails
84
+ test_files: []