notebook 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: b3b29fcabdf77d11419e6d32a2de2d043a477dee
4
+ data.tar.gz: b858be60c3f58783f527a18f97505cd316406445
5
+ SHA512:
6
+ metadata.gz: 245f639d4f1a9e72c6b1f91a7b62424ba54d3b68103bbe042ae675bc8dc0e53c7415ebdfb80973435a9863a9fd45cd1acda8c05a000a31e53823e571d32dfe07
7
+ data.tar.gz: 4b2dacc69de34a410005c9ce54a9636e1f964581154aa193ba2c17cedbfbdbe661af8efd48309807053259570baf787b6908dd253f16ed5d915bf26e0bd7df8b
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/fixtures/public/
9
+ /spec/rails/db/
10
+ /spec/reports/
11
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1 @@
1
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,10 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+ Style/DoubleNegation:
4
+ Enabled: false
5
+ Style/LineLength:
6
+ Max: 110
7
+ Style/MethodLength:
8
+ Max: 15
9
+ Style/SymbolProc:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in notebook.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jon Moss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Notebook
2
+
3
+ [![Build Status](https://travis-ci.org/moss-rb/notebook.svg?branch=master)](https://travis-ci.org/moss-rb/notebook?branch=master)
4
+
5
+ Notebook is a Ruby file attachment library, that has easy integration
6
+ with Rails and ActiveRecord.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'notebook'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install notebook
23
+
24
+ ## Rails Quickstart
25
+
26
+ ### Models
27
+
28
+ **Rails 4**
29
+
30
+ ```ruby
31
+ class User < ActiveRecord::Base
32
+ notebook_attachment :profile_picture
33
+ end
34
+ ```
35
+
36
+ ### Edit and New Views
37
+
38
+ ```erb
39
+ <%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
40
+ <%= form.file_field :avatar %>
41
+ <% end %>
42
+ ```
43
+
44
+ ### Controller
45
+
46
+ **Rails 4**
47
+
48
+ ```ruby
49
+ def create
50
+ @user = User.create(user_params)
51
+ end
52
+
53
+ private
54
+ def user_params
55
+ params.require(:user).permit(:avatar)
56
+ end
57
+ ```
58
+
59
+ ### Show View
60
+
61
+ ```erb
62
+ <%= image_tag @user.avatar.url %>
63
+ ```
64
+
65
+ ### Deleting an Attachment
66
+
67
+ Set the attribute to `nil` and save.
68
+
69
+ ```ruby
70
+ @user.avatar = nil
71
+ @user.save
72
+ ```
73
+
74
+ ### Configuration
75
+ To use a different storage location than /public, create a
76
+ `config/initializers/notebook.rb`, and then set the following:
77
+
78
+ ```ruby
79
+ Notebook.public_directory = 'hi'
80
+ ```
81
+
82
+ ## Plain Old Ruby
83
+
84
+ It's awesome you want to use plain Ruby without Rails with Notebook!
85
+
86
+ First off, you need to read in a file, and create a new
87
+ `Notebook::Attachment` object, like so:
88
+
89
+ ```ruby
90
+ file = File.read("avatar.jpg")
91
+ attachment = Notebook::Attachment.new(file)
92
+ ```
93
+
94
+ The last and final step is to just upload it!
95
+ ```ruby
96
+ attachment.upload
97
+
98
+ # get the url of the newly persisted file
99
+ attachment.url
100
+ ```
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moss-rb/notebook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
105
+
106
+
107
+ ## License
108
+
109
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+
5
+ # RSpec Rake Task
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ # Rubocop Rake Task
10
+ require 'rubocop/rake_task'
11
+ RuboCop::RakeTask.new
12
+
13
+ task default: [:rubocop, :spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'notebook'
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ module Notebook
2
+ class Attachment
3
+ attr_reader :file
4
+
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def delete
10
+ adapter.delete
11
+ end
12
+
13
+ def upload
14
+ adapter.upload
15
+ end
16
+
17
+ def url
18
+ adapter.url
19
+ end
20
+
21
+ private
22
+
23
+ def adapter
24
+ StorageAdapters::Filesystem.new(self)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require 'active_record'
2
+
3
+ module Notebook
4
+ module ActiveRecordAttachment
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ # Attachment method which hooks into ActiveRecord models
9
+ def notebook_attachment(name)
10
+ define_method "#{name}" do
11
+ @notebook_attachment
12
+ end
13
+
14
+ define_method "#{name}=" do |value|
15
+ @notebook_attachment = Notebook::Attachment.new(value)
16
+ @notebook_attachment.upload
17
+ @notebook_attachment
18
+ end
19
+
20
+ after_destroy do
21
+ @notebook_attachment.delete
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ ::ActiveRecord::Base.send(:include, Notebook::ActiveRecordAttachment)
@@ -0,0 +1,9 @@
1
+ module Notebook
2
+ class Engine < ::Rails::Engine
3
+ initializer 'notebook.setup' do
4
+ ActiveSupport.on_load :active_record do
5
+ require 'notebook/rails/active_record'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ module Notebook
2
+ module StorageAdapters
3
+ class Base
4
+ def initialize(attachment, _options = {})
5
+ @attachment = attachment
6
+ end
7
+
8
+ def delete
9
+ fail(
10
+ NotImplementedError,
11
+ 'Compliant Notebook storage adapters should define the `delete` method'
12
+ )
13
+ end
14
+
15
+ def get
16
+ fail(
17
+ NotImplementedError,
18
+ 'Compliant Notebook storage adapters should define the `get` method'
19
+ )
20
+ end
21
+
22
+ def upload
23
+ fail(
24
+ NotImplementedError,
25
+ 'Compliant Notebook storage adapters should define the `upload` method'
26
+ )
27
+ end
28
+
29
+ protected
30
+
31
+ attr_reader :attachment
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'fileutils'
2
+
3
+ module Notebook
4
+ module StorageAdapters
5
+ class Filesystem < Base
6
+ def initialize(attachment, options = {})
7
+ super
8
+ @storage_directory = options.fetch('storage_directory', Notebook.public_directory)
9
+ end
10
+
11
+ def delete
12
+ !!FileUtils.remove(url)
13
+ end
14
+
15
+ def upload
16
+ # When this command is successful, it returns nil
17
+ FileUtils.copy(attachment.file, @storage_directory).nil?
18
+ end
19
+
20
+ def url
21
+ (@storage_directory + File.basename(attachment.file)).to_s
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Notebook
2
+ module StorageAdapters
3
+ autoload :Base, 'notebook/storage_adapters/base'
4
+ autoload :Filesystem, 'notebook/storage_adapters/filesystem'
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module Notebook
2
+ VERSION = '0.1.0'
3
+ end
data/lib/notebook.rb ADDED
@@ -0,0 +1,19 @@
1
+ require 'pathname'
2
+ require 'notebook/version'
3
+
4
+ require 'notebook/rails/engine' if defined?(Rails)
5
+
6
+ module Notebook
7
+ autoload :Attachment, 'notebook/attachment'
8
+ autoload :StorageAdapters, 'notebook/storage_adapters'
9
+
10
+ # Return the current filesystem storage directory in use
11
+ def self.public_directory
12
+ @public_directory || (Pathname.pwd + 'public')
13
+ end
14
+
15
+ # Set the storage filesystem directory to use
16
+ def self.public_directory=(directory)
17
+ @public_directory = directory
18
+ end
19
+ end
data/notebook.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'notebook/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'notebook'
8
+ spec.version = Notebook::VERSION
9
+ spec.authors = 'Jon Moss'
10
+ spec.email = 'me@jonathanmoss.me'
11
+
12
+ spec.summary = 'A simple Ruby file uploader library'
13
+ spec.description = 'A simple Ruby file uploader library'
14
+ spec.homepage = 'https://github.com/moss-rb/notebook'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.10'
23
+ spec.add_development_dependency 'rails'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'sqlite3'
28
+ end
data/roadmap.txt ADDED
@@ -0,0 +1,6 @@
1
+ * Global gem configuration
2
+ * filesystem --> storage_directory in FilesystemAdapter should be configurable (now it's hardwired to write in spec/fixtures)
3
+ * Rails helpers
4
+ * `before_upload` and `after_upload`
5
+ * S3 StorageAdapter
6
+ * refactor tests per Tute
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notebook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Moss
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-21 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
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: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A simple Ruby file uploader library
98
+ email: me@jonathanmoss.me
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - ".rubocop_todo.yml"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/notebook.rb
116
+ - lib/notebook/attachment.rb
117
+ - lib/notebook/rails/active_record.rb
118
+ - lib/notebook/rails/engine.rb
119
+ - lib/notebook/storage_adapters.rb
120
+ - lib/notebook/storage_adapters/base.rb
121
+ - lib/notebook/storage_adapters/filesystem.rb
122
+ - lib/notebook/version.rb
123
+ - notebook.gemspec
124
+ - roadmap.txt
125
+ homepage: https://github.com/moss-rb/notebook
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.8
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: A simple Ruby file uploader library
149
+ test_files: []