photofy 0.0.1

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.
Files changed (2) hide show
  1. data/lib/photofy.rb +91 -0
  2. metadata +53 -0
@@ -0,0 +1,91 @@
1
+ module Photofy
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ attr_accessor :photofield_flag
8
+ attr_accessor :photo_field
9
+ attr_accessor :photo_repository
10
+ attr_accessor :photo_formats
11
+
12
+ #Getter to check if model is enabled with photofied
13
+ def is_photofield?
14
+ @photofield_flag.nil? ? false : @photofield_flag
15
+ end
16
+
17
+ def register_callbacks
18
+ send(:after_save, "#{photo_field}_store!")
19
+ send(:after_destroy, "#{photo_field}_destroy!")
20
+ end
21
+
22
+ #Generates photo filed from photo_field arguments and provides methods like
23
+ #if photo_filed is "collage" then it provides methods on top of it as
24
+ #collage -> Getter,
25
+ #collage = -> Setter. Accepted inputs are file upload(ActionDispatch::Http::UploadedFile), filer handle and String(no format validation is ignored),
26
+ #collage_path -> Getter of filepath,
27
+ #collage_persisted? -> true if provided file/data is stored on disk,
28
+ #collage_store! -> to store provided file/data on disk,
29
+ #collage_destroy! -> to store destroy stored file/data from disk
30
+ def photofy(photo_filed, options = {})
31
+ if options.is_a?(Hash)
32
+ @photo_formats = options[:formats].is_a?(Array) ? options[:formats].collect { |x| x.starts_with?(".") ? x : ".#{x}" } : [".bmp", ".jpg", ".jpeg"]
33
+ else
34
+ raise "InvalidArguments"
35
+ end
36
+
37
+ @photo_field = photo_filed
38
+
39
+ register_callbacks
40
+
41
+ define_method "#{@photo_field}" do
42
+ @file_buffer.nil? ? (send("#{self.class.photo_field}_persisted?") ? File.read(send("#{self.class.photo_field}_path")) : nil) : @file_buffer
43
+ end
44
+
45
+ define_method "#{@photo_field}=" do |file_upload|
46
+ if file_upload.class == ActionDispatch::Http::UploadedFile
47
+ return false unless self.class.photo_formats.include?(File.extname(file_upload.original_filename).downcase)
48
+ @file_buffer = File.read(file_upload.path)
49
+ elsif file_upload.class == File
50
+ return false unless self.class.photo_formats.include?(File.extname(file_upload.path).downcase)
51
+ @file_buffer = file_upload.read
52
+ elsif file_upload.class == String
53
+ #return false unless self.class.photo_formats.include?(File.extname(file_upload).downcase)
54
+ @file_buffer = file_upload
55
+ end
56
+ end
57
+
58
+ define_method "#{@photo_field}_path" do
59
+ directoy_path = FileUtils.mkdir_p File.join(self.class.photo_repository, self.class.photo_field.to_s)
60
+ File.join(directoy_path, self.send(self.class.primary_key).to_s)
61
+ end
62
+
63
+ define_method "#{@photo_field}_persisted?" do
64
+ (@file_buffer.nil? and File.exist?(send("#{self.class.photo_field}_path")))
65
+ end
66
+
67
+ define_method "#{@photo_field}_store!" do
68
+ return unless self.class.is_photofield?
69
+ unless @file_buffer.nil?
70
+ File.open(send("#{self.class.photo_field}_path"), "wb+") { |f| f.puts(@file_buffer) }
71
+ @file_buffer = nil
72
+ end
73
+ end
74
+
75
+ define_method "#{@photo_field}_destroy!" do
76
+ return unless self.class.is_photofield?
77
+ @file_buffer = nil
78
+ File.delete(send("#{self.class.photo_field}_path")) if File.exist?(send("#{self.class.photo_field}_path"))
79
+ end
80
+
81
+ @photofield_flag = true
82
+ end
83
+
84
+ def photo_repository
85
+ @photo_repository ||= FileUtils.mkdir_p File.join(Rails.root, "photofied", self.name)
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ ActiveRecord::Base.send(:include, Photofy)
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: photofy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Praveen Kumar Sinha
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "A gem to provide simple method to do fileupload of pictures and provides
15
+ getter setter methods of it and save on model object commit.\n #Generates photo
16
+ filed from photo_field arguments and provides methods like\n #if photo_filed
17
+ is \"collage\" then it provides methods on top of it as\n #collage -> Getter,\n
18
+ \ #collage = -> Setter. Accepted inputs are file upload(ActionDispatch::Http::UploadedFile),
19
+ filer handle and String(no format validation is ignored),\n #collage_path ->
20
+ Getter of filepath,\n #collage_persisted? -> true if provided file/data is stored
21
+ on disk,\n #collage_store! -> to store provided file/data on disk,\n #collage_destroy!
22
+ -> to store destroy stored file/data from disk"
23
+ email: praveen.kumar.sinha@gmail.com
24
+ executables: []
25
+ extensions: []
26
+ extra_rdoc_files: []
27
+ files:
28
+ - lib/photofy.rb
29
+ homepage: https://github.com/praveenkumarsinha/Photofy
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.24
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Photofy
53
+ test_files: []