acts_as_file 0.0.1 → 0.0.2

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: 5a692309dc92a0a6668370bdf0d1a750c41372dd
4
- data.tar.gz: 7a8957cf252f0d825fad043b8a2235f9561ef97f
3
+ metadata.gz: 6ac43e472974779df59d6ca89dd2b5511af346a1
4
+ data.tar.gz: 56eedfba6276c82ebce6329b60a8cd3822a76d3e
5
5
  SHA512:
6
- metadata.gz: 6ddfc4ad98004d6ab638aa198abaca8d0b7aec385d4cd6f0a7af4148921b0dd22a9065cdb6b38e00ffd71566134463b7082cf18d4a97e285b89641ce007702be
7
- data.tar.gz: dd63c176af3007665c0dec56812d108749ce3ab2bd042303c8786c47d6c79ab5999b75e99ab2d1fdf4763f749892dda7e3d546a5c6921951ad1214847bbca30b
6
+ metadata.gz: 9a2d7666e1d2440226a66fdf9034cb9e4ced59f0b0b0a5c62616fa900a16ca3e513f7fbb9cde98552d8e2892ba70741847f90c8e291314824959e3ed9407b286
7
+ data.tar.gz: 5af73ab836bd7dabdaa75c7922eb03ea6f0cfd2eb1d289e30cccff2fcdf7b21250acc9df0c3587c4e34f23c3ba567fd8cc210df693760edcf32ff66961a702f3
@@ -2,7 +2,7 @@ rvm:
2
2
  - 1.9.3
3
3
  - 2.0.0
4
4
  - 2.1.0
5
- - rbx
5
+ - rbx-2
6
6
  - jruby-19mode
7
7
  gemfile:
8
8
  - Gemfile
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 # save the content into the file of `#filename`
40
- loaded = Post.find(post.id)
41
- puts loaded.body # load the content from the file of `#filename`
42
- loaded.destroy # remove the file
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
@@ -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.1'
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"]
@@ -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
- File.open(filename, 'w') do |f|
18
- f.flock(File::LOCK_EX) # inter-process locking
19
- f.sync = true
20
- f.write(content)
21
- f.flush
22
- end if filename and content
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
@@ -1,27 +1,8 @@
1
1
  require_relative 'spec_helper'
2
- require 'acts_as_file'
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(name: 'name') }
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
@@ -0,0 +1,9 @@
1
+ require 'acts_as_file'
2
+
3
+ class TestPost
4
+ include ActsAsFile
5
+ def filename
6
+ @filename ||= Tempfile.open('test_acts_as_file') {|f| f.path }.tap {|name| File.unlink(name) }
7
+ end
8
+ acts_as_file :body => self.instance_method(:filename)
9
+ end
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.1
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-26 00:00:00.000000000 Z
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