Module: Refile::BackendMacros Private

Included in:
Refile::Backend::FileSystem
Defined in:
lib/refile/backend_macros.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Macros which make it easier to write secure backends.

Instance Method Summary collapse

Instance Method Details

#decode_id(id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


41
42
43
# File 'lib/refile/backend_macros.rb', line 41

def decode_id(id)
  id.to_s
end

#valid_id?(id) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

37
38
39
# File 'lib/refile/backend_macros.rb', line 37

def valid_id?(id)
  id =~ /\A[a-z0-9]+\z/i
end

#verify_id(method) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/refile/backend_macros.rb', line 6

def verify_id(method)
  mod = Module.new do
    define_method(method) do |id|
      id = self.class.decode_id(id)
      if self.class.valid_id?(id)
        super(id)
      else
        raise Refile::InvalidID
      end
    end
  end
  prepend mod
end

#verify_uploadable(method) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/refile/backend_macros.rb', line 20

def verify_uploadable(method)
  mod = Module.new do
    define_method(method) do |uploadable|
      [:size, :read, :eof?, :rewind, :close].each do |m|
        unless uploadable.respond_to?(m)
          raise Refile::InvalidFile, "does not respond to `#{m}`."
        end
      end
      if max_size and uploadable.size > max_size
        raise Refile::InvalidMaxSize, "#{uploadable.inspect} is too large"
      end
      super(uploadable)
    end
  end
  prepend mod
end