acts_as_file 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/README.md +7 -4
- data/acts_as_file.gemspec +1 -1
- data/lib/acts_as_file.rb +14 -6
- data/spec/acts_as_file_spec.rb +2 -21
- data/spec/model.rb +9 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ac43e472974779df59d6ca89dd2b5511af346a1
|
4
|
+
data.tar.gz: 56eedfba6276c82ebce6329b60a8cd3822a76d3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a2d7666e1d2440226a66fdf9034cb9e4ced59f0b0b0a5c62616fa900a16ca3e513f7fbb9cde98552d8e2892ba70741847f90c8e291314824959e3ed9407b286
|
7
|
+
data.tar.gz: 5af73ab836bd7dabdaa75c7922eb03ea6f0cfd2eb1d289e30cccff2fcdf7b21250acc9df0c3587c4e34f23c3ba567fd8cc210df693760edcf32ff66961a702f3
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -34,12 +34,15 @@ class Post < ActiveRecord::Base
|
|
34
34
|
acts_as_file :body => self.instance_method(:filename)
|
35
35
|
end
|
36
36
|
|
37
|
+
# store
|
37
38
|
post = Post.new
|
38
39
|
post.body = 'content'
|
39
|
-
post.save
|
40
|
-
|
41
|
-
|
42
|
-
|
40
|
+
post.save # save the content into the file of `#filename`
|
41
|
+
# create the directory if not exist
|
42
|
+
# load
|
43
|
+
post = Post.first
|
44
|
+
puts post.body # load the content from the file of `#filename`
|
45
|
+
post.destroy # remove the file
|
43
46
|
```
|
44
47
|
|
45
48
|
## Contributing
|
data/acts_as_file.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.homepage = "https://github.com/sonots/acts_as_file"
|
9
9
|
s.summary = "Make your field act as a file"
|
10
10
|
s.description = "Make your field act as a file. Save the content to a file, and load the content from a file."
|
11
|
-
s.version = '0.0.
|
11
|
+
s.version = '0.0.2'
|
12
12
|
s.date = Time.now.strftime("%Y-%m-%d")
|
13
13
|
|
14
14
|
s.extra_rdoc_files = Dir["*.rdoc"]
|
data/lib/acts_as_file.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
1
3
|
module ActsAsFile
|
2
4
|
def self.included(klass)
|
3
5
|
klass.extend(ClassMethods)
|
@@ -14,15 +16,20 @@ module ActsAsFile
|
|
14
16
|
field_name = :"@#{field}"
|
15
17
|
filename = filename_instance_method.bind(self).call
|
16
18
|
content = self.instance_variable_get(field_name)
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
if filename and content
|
20
|
+
dirname = File.dirname(filename)
|
21
|
+
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)
|
22
|
+
File.open(filename, 'w') do |f|
|
23
|
+
f.flock(File::LOCK_EX) # inter-process locking
|
24
|
+
f.sync = true
|
25
|
+
f.write(content)
|
26
|
+
f.flush
|
27
|
+
end
|
28
|
+
end
|
23
29
|
end
|
24
30
|
save_without_file(*args)
|
25
31
|
end
|
32
|
+
define_method(:save) {|*args| } unless method_defined?(:save)
|
26
33
|
alias_method :save_without_file, :save
|
27
34
|
alias_method :save, :save_with_file
|
28
35
|
|
@@ -65,6 +72,7 @@ module ActsAsFile
|
|
65
72
|
end
|
66
73
|
destroy_without_file
|
67
74
|
end
|
75
|
+
define_method(:destroy) {} unless method_defined?(:destroy)
|
68
76
|
alias_method :destroy_without_file, :destroy
|
69
77
|
alias_method :destroy, :destroy_with_file
|
70
78
|
end
|
data/spec/acts_as_file_spec.rb
CHANGED
@@ -1,27 +1,8 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
class Post
|
5
|
-
def initialize(params)
|
6
|
-
@name = params[:name]
|
7
|
-
end
|
8
|
-
attr_accessor :name
|
9
|
-
def save; end
|
10
|
-
def destroy; end
|
11
|
-
def self.delete_all; end
|
12
|
-
end
|
13
|
-
|
14
|
-
class TestPost < Post
|
15
|
-
include ActsAsFile
|
16
|
-
def filename
|
17
|
-
@filename ||= Tempfile.open(self.name) {|f| f.path }.tap {|name| File.unlink(name) }
|
18
|
-
end
|
19
|
-
acts_as_file :body => self.instance_method(:filename)
|
20
|
-
end
|
2
|
+
require_relative 'model'
|
21
3
|
|
22
4
|
describe ActsAsFile do
|
23
|
-
let(:subject) { TestPost.new
|
24
|
-
after { TestPost.delete_all }
|
5
|
+
let(:subject) { TestPost.new }
|
25
6
|
after { File.unlink(subject.filename) if File.exist?(subject.filename) }
|
26
7
|
|
27
8
|
context '#body=' do
|
data/spec/model.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_file
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Naotoshi Seo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- acts_as_file.gemspec
|
84
84
|
- lib/acts_as_file.rb
|
85
85
|
- spec/acts_as_file_spec.rb
|
86
|
+
- spec/model.rb
|
86
87
|
- spec/spec_helper.rb
|
87
88
|
homepage: https://github.com/sonots/acts_as_file
|
88
89
|
licenses: []
|
@@ -110,4 +111,5 @@ specification_version: 4
|
|
110
111
|
summary: Make your field act as a file
|
111
112
|
test_files:
|
112
113
|
- spec/acts_as_file_spec.rb
|
114
|
+
- spec/model.rb
|
113
115
|
- spec/spec_helper.rb
|