Module: Balloon::Up::ClassMethods

Defined in:
lib/balloon/up.rb

Instance Method Summary collapse

Instance Method Details

#set_keywordObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/balloon/up.rb', line 46

def set_keyword
  if defined?(MongoMapper)
    key :file_name, String
    key :width, Integer
    key :height, Integer
    key :content_type, String
    key :file_size, Integer
    key :storage, String
    key :metadata
    key :created_at
  elsif defined?(Mongoid)
    field :file_name, type: String
    field :width, type: Integer
    field :height, type: Integer
    field :content_type, type: String
    field :file_size, type: String
    field :storage, type: String
    field :metadata
    field :created_at
  end

  before_create :save_db
  after_destroy :uploader_delete

  class_eval <<-RUBY
    def save_db
      return if meta.nil?
      self.file_name = meta[:filename]
      self.content_type = meta[:mime_type]
      self.file_size = meta[:size]
      self.storage = store_storage.to_s
      self.width = meta[:width]
      self.height = meta[:height]
      if self.respond_to?(:metadata)
        self.metadata = meta[:data]
      end
    end

    def url(size_name = nil)
     return "" if !respond_to?(:file_name) || file_name.nil?
     extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
     basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
     @meta = { basename: basename, extension: extension.to_s }
     set_storage_engine
     storage_engine.retrieve!(size_name)
    end

    def path(size_name = nil)
     return "" if !respond_to?(:file_name) || file_name.nil?
     extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
     basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
     @meta = { basename: basename, extension: extension.to_s }
     set_storage_engine
     storage_engine.path!(size_name)
    end

    def local_path(size_name = nil)
     return "" if !respond_to?(:file_name) || file_name.nil?
     extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
     basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
     @meta = { basename: basename, extension: extension.to_s }
     set_storage_engine
     storage_engine.local_path!(size_name)
    end

    def uploader_delete
     return if !respond_to?(:file_name) || file_name.nil?
     extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
     basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
     @meta = { basename: basename, extension: extension.to_s }
     set_storage_engine
     storage_engine.delete!
    end
  RUBY
end

#uploader(name, db = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/balloon/up.rb', line 11

def uploader(name, db = nil)

  before_create :uploader_save

  class_eval <<-RUBY
    def #{name}=(file)
      save_to_cache(file)
    rescue DownloadError => e
      @download_error = e
    rescue ProcessError => e
      @process_error = e
    end

    def #{name}
      @meta
    end

    def uploader_save
      return if cache_meta.nil?
      set_storage_engine
      store_info = storage_engine.store!
      @meta[:filename] = store_info[:filename]
      @meta[:basename] = store_info[:basename]
    end

    def uploader_name
      "#{name}".pluralize
    end
  RUBY

  set_keyword if db

  validates_download_of name
end