has_file 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/has_file.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'has_file/class_methods'
2
+ require 'has_file/instance_methods'
3
+
4
+ module HasFile
5
+ require 'rails'
6
+
7
+ class Railtie < Rails::Railtie
8
+ initializer 'HasFile' do
9
+ ActiveRecord::Base.send :extend, HasFile::ClassMethods
10
+ ActiveRecord::Base.send :include, HasFile::InstanceMethods
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module HasFile
2
+ module ClassMethods
3
+ def has_file(name)
4
+ define_method name do |*args|
5
+ retrieve_file name
6
+ end
7
+
8
+ define_method "#{name}=" do |file|
9
+ assign_file name, file
10
+ end
11
+
12
+ define_method "#{name}?" do
13
+ has_file? name
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module HasFile
2
+ module InstanceMethods
3
+ def assign_file(name, file)
4
+ write_attribute "#{name}_file", file.read
5
+
6
+ if self.respond_to? "#{name}_file_name"
7
+ write_attribute "#{name}_file_name", file.original_filename
8
+ end
9
+
10
+ if self.respond_to? "#{name}_content_type"
11
+ write_attribute "#{name}_content_type", file.content_type.to_s.strip
12
+ end
13
+ end
14
+
15
+ def retrieve_file(name)
16
+ if has_file?(name)
17
+ reader, writer = IO.pipe
18
+ writer.write self.send("#{name}_file")
19
+ writer.close
20
+ reader
21
+ end
22
+ end
23
+
24
+ def has_file?(name)
25
+ !self.send("#{name}_file").nil?
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Treatman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Makes it easy to save uploaded files in the database with ActiveRecord
15
+ email: charles.treatman@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/has_file.rb
21
+ - lib/has_file/class_methods.rb
22
+ - lib/has_file/instance_methods.rb
23
+ homepage: http://rubygems.org/gems/has_file
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.21
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: has_file
47
+ test_files: []