file_model 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ class FileModel::Adapter
2
+ def initialize something
3
+ @something = something
4
+ end
5
+
6
+ def to_file
7
+ self.class.clear_expired_cache
8
+
9
+ if something.respond_to? :to_file
10
+ something.to_file
11
+ elsif something.is_a? Hash
12
+ data = something["tempfile"] || something[:tempfile] || raise("no file!")
13
+ name = something["filename"] || something[:filename] || raise("no filename!")
14
+ file = "#{self.class.cache_dir}/#{name}".to_file
15
+ file.write data.read
16
+ file
17
+ elsif something.is_a? File
18
+ path = something.path
19
+ name = File.basename path
20
+ file = "#{self.class.cache_dir}/#{name}".to_file
21
+ file.write something.read
22
+ file
23
+ else
24
+ raise "unknown file format!"
25
+ end
26
+ end
27
+
28
+ EXPIRATION_TIME = 5
29
+ BASE_DIR = "/tmp/file_model_cache"
30
+
31
+ class << self
32
+ def cache_dir
33
+ "#{BASE_DIR}/#{Time.now.min}/#{rand(1_000_000)}"
34
+ end
35
+
36
+ def clear_expired_cache
37
+ time = Time.now.min
38
+ dir = BASE_DIR.to_dir
39
+ if dir.exist?
40
+ dir.to_dir.entries do |entry|
41
+ entry_time = entry.name.to_i
42
+ entry.destroy if (entry_time > time) or (time > (entry_time + EXPIRATION_TIME))
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ protected
49
+ attr_reader :something
50
+ end
@@ -8,13 +8,13 @@ module FileModel
8
8
 
9
9
  attr_reader :original
10
10
  def original= something
11
- @original = something.to_file
12
- raise "file #{original.name} not exist!" if original and !original.exist?
11
+ @original = Adapter.new(something).to_file
12
+ raise "file #{original.name} not exist!" unless original.exist?
13
13
  original
14
14
  end
15
15
 
16
16
  def save options = {}
17
- return false unless original
17
+ return true unless original
18
18
 
19
19
  # validating
20
20
  return false unless options[:validate] == false or valid?
@@ -24,6 +24,7 @@ module FileModel
24
24
 
25
25
  # saving
26
26
  self.name = build_name original.name
27
+
27
28
  process do |file|
28
29
  path = build_path name
29
30
  file.copy_to self.class.box[path]
@@ -41,11 +42,11 @@ module FileModel
41
42
  end
42
43
 
43
44
  def save! options = {}
44
- save(options) || raise("can't save #{self}!")
45
+ save(options) || raise("can't save #{self} (#{errors.inspect})!")
45
46
  end
46
47
 
47
48
  def destroy options = {}
48
- return false unless name
49
+ return true unless name
49
50
 
50
51
  return false unless options[:validate] == false or valid?
51
52
 
@@ -61,7 +62,7 @@ module FileModel
61
62
  end
62
63
 
63
64
  def destroy! options = {}
64
- destroy(options) || raise("can't destroy #{self}!")
65
+ destroy(options) || raise("can't destroy #{self} (#{errors.inspect})!")
65
66
  end
66
67
 
67
68
  def url
@@ -76,15 +77,17 @@ module FileModel
76
77
  block.call original if original
77
78
  end
78
79
 
79
- def build_path name, version = nil
80
+ def build_standard_path name, version = nil
80
81
  base, extension = name.rsplit('.', 2)
81
82
  %(/#{base}#{".#{version}" if version}#{".#{extension}" if extension})
82
83
  end
84
+ alias_method :build_path, :build_standard_path
83
85
 
84
- def build_url name, version = nil
86
+ def build_standard_url name, version = nil
85
87
  base, extension = name.rsplit('.', 2)
86
88
  %(/#{base}#{".#{version}" if version}#{".#{extension}" if extension})
87
89
  end
90
+ alias_method :build_url, :build_standard_url
88
91
 
89
92
  def build_name name
90
93
  name
@@ -103,6 +106,10 @@ module FileModel
103
106
  def run_validations; end
104
107
 
105
108
  class << self
109
+ def box name
110
+ raise "Override this method to provide Your own custom box initialization."
111
+ end
112
+
106
113
  attr_accessor :box
107
114
  attr_required :box
108
115
  end
@@ -127,19 +134,12 @@ module FileModel
127
134
  end
128
135
  end
129
136
 
130
- inheritable_accessor :_box, nil
131
- def box= v
132
- self._box = v.is_a?(::Proc) ? v : -> {v}
133
- end
134
-
135
- def box *args, &block
136
- if block
137
- self.box = block
138
- elsif !args.empty?
139
- args.size.must == 1
140
- self.box = args.first
137
+ inheritable_accessor :box_name, :default
138
+ def box name = nil
139
+ if name
140
+ self.box_name = name
141
141
  else
142
- (_box && _box.call) || ::FileModel.box
142
+ FileModel.box box_name
143
143
  end
144
144
  end
145
145
  end
@@ -3,7 +3,7 @@ module FileModel::MiniMagic
3
3
  return unless original
4
4
 
5
5
  require 'mini_magick'
6
- image = MiniMagick::Image.open(original.file.path)
6
+ image = MiniMagick::Image.open(original.path)
7
7
  block.call image
8
8
  '/'.to_entry.tmp do |dir|
9
9
  output = dir / :tmp_image
@@ -1,24 +1,26 @@
1
1
  require 'file_model'
2
2
  require 'vos'
3
3
 
4
+ FileModel::ClassMethods.class_eval do
5
+ def box; FileModel.box end
6
+ end
7
+
8
+ FileModel.metaclass_eval do
9
+ attr_accessor :box
10
+ end
11
+
4
12
  rspec do
5
13
  def file_model_storage
6
- '/tmp/file_model'.to_dir
14
+ '/tmp/file_model_test'.to_dir
7
15
  end
8
16
 
9
17
  class << self
10
18
  def with_file_model
11
- ::FileModel::ClassMethods.class_eval do
12
- def box
13
- ::FileModel.box
14
- end
15
- end
16
-
17
- tmp = '/tmp/file_model'.to_dir
19
+ tmp = '/tmp/file_model_test'.to_dir
18
20
 
19
21
  before do
20
22
  tmp.destroy.create
21
- FileModel.box = Vos::Box.new(Vos::Drivers::Local.new(tmp.path))
23
+ FileModel.box = Vos::Box.new(Vos::Drivers::Local.new(root: tmp.path))
22
24
  end
23
25
 
24
26
  after{tmp.destroy}
data/lib/file_model.rb CHANGED
@@ -7,6 +7,7 @@ require 'ruby_ext'
7
7
  gems
8
8
  version
9
9
  helper
10
+ adapter
10
11
  file_model
11
12
  mini_magic
12
13
  ).each{|f| require "file_model/#{f}"}
Binary file
@@ -0,0 +1 @@
1
+ text
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "File Model" do
6
+ with_file_model
7
+
8
+ it 'should accept different formats' do
9
+ original = FileModel::Adapter.new("#{spec_dir}/file.txt").to_file
10
+ original.name.should == 'file.txt'
11
+ original.read.should == "text"
12
+
13
+ original = FileModel::Adapter.new("#{spec_dir}/file.txt".to_file).to_file
14
+ original.name.should == 'file.txt'
15
+ original.read.should == "text"
16
+
17
+ File.open "#{spec_dir}/file.txt" do |f|
18
+ original = FileModel::Adapter.new(f).to_file
19
+ original.name.should == 'file.txt'
20
+ original.read.should == "text"
21
+ end
22
+
23
+ File.open "#{spec_dir}/file.txt" do |f|
24
+ original = FileModel::Adapter.new(filename: 'file.txt', tempfile: f).to_file
25
+ original.name.should == 'file.txt'
26
+ original.read.should == "text"
27
+ end
28
+ end
29
+
30
+ it 'work with binary format' do
31
+ File.open "#{spec_dir}/bos.jpeg" do |f|
32
+ original = FileModel::Adapter.new(filename: 'bos.jpeg', tempfile: f).to_file
33
+ original.name.should == 'bos.jpeg'
34
+ original.size.should > 0
35
+ end
36
+ end
37
+ end
@@ -20,11 +20,11 @@ describe "File Model" do
20
20
  end
21
21
 
22
22
  def build_path name, version = nil
23
- '/storage/images' + super
23
+ '/storage/images' + build_standard_path(name, version)
24
24
  end
25
25
 
26
26
  def build_url name, version = nil
27
- '/images' + super
27
+ '/images' + build_standard_url(name, version)
28
28
  end
29
29
 
30
30
  def build_name name
@@ -158,15 +158,4 @@ describe "File Model" do
158
158
  image.save.should be_false
159
159
  image.errors.should == ['invalid name']
160
160
  end
161
-
162
- it 'should accept anything responding to :to_file' do
163
- file = "#{spec_dir}/ship.jpg".to_file
164
-
165
- obj = mock
166
- obj.should_receive(:to_file).and_return(file)
167
-
168
- image = ImageFile.new
169
- image.original = obj
170
- image.original.should == file
171
- end
172
161
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-06 00:00:00.000000000Z
12
+ date: 2011-09-15 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - Rakefile
21
21
  - readme.md
22
+ - lib/file_model/adapter.rb
22
23
  - lib/file_model/file_model.rb
23
24
  - lib/file_model/gems.rb
24
25
  - lib/file_model/helper.rb
@@ -31,6 +32,9 @@ files:
31
32
  - lib/file_model/version.rb
32
33
  - lib/file_model.rb
33
34
  - spec/abstract_model_spec.rb
35
+ - spec/adapter_spec/bos.jpeg
36
+ - spec/adapter_spec/file.txt
37
+ - spec/adapter_spec.rb
34
38
  - spec/file_model_spec/file+name.txt
35
39
  - spec/file_model_spec/invalid.txt
36
40
  - spec/file_model_spec/ship.jpg