Module: Attachinary::Utils

Defined in:
lib/attachinary/utils.rb

Class Method Summary collapse

Class Method Details

.process_hash(hash, scope = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/attachinary/utils.rb', line 10

def self.process_hash(hash, scope=nil)
  if hash['id']
    Attachinary::File.find hash['id']
  else
    file = Attachinary::File.new hash.slice(*Attachinary::File.attr_accessible[:default].to_a)
    file.scope = scope.to_s if scope && file.respond_to?(:scope=)
    file
  end
end

.process_input(input, scope = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/attachinary/utils.rb', line 21

def self.process_input(input, scope=nil)
  case input
  when :blank?.to_proc
    nil
  when lambda { |e| e.respond_to?(:read) }
    process_hash Cloudinary::Uploader.upload(input, resource_type: 'auto'), scope
  when String
    process_json(input, scope)
  when Hash
    process_hash(input, scope)
  when Array
    input = input.map{ |el| process_input(el, scope) }.flatten.compact
    input = nil if input.empty?
    input
  else
    input
  end
end

.process_json(json, scope = nil) ⇒ Object



4
5
6
7
8
# File 'lib/attachinary/utils.rb', line 4

def self.process_json(json, scope=nil)
  [JSON.parse(json)].flatten.compact.map do |data|
    process_hash(data, scope)
  end
end

.process_options(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/attachinary/utils.rb', line 40

def self.process_options(options)
  options = options.reverse_merge({
    accessible: true
  })
  options[:maximum] = 1 if options[:single]

  if options[:single]
    options[:singular] = options[:scope].to_s
    options[:plural] = options[:scope].to_s.pluralize
  else
    options[:plural] = options[:scope].to_s
    options[:singular] = options[:scope].to_s.singularize
  end

  options
end