Class: Saviour::Integrator

Inherits:
Object
  • Object
show all
Defined in:
lib/saviour/integrator.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, persistence_klass) ⇒ Integrator

Returns a new instance of Integrator.



3
4
5
6
# File 'lib/saviour/integrator.rb', line 3

def initialize(klass, persistence_klass)
  @klass = klass
  @persistence_klass = persistence_klass
end

Instance Method Details

#setup!Object



8
9
10
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/saviour/integrator.rb', line 8

def setup!
  raise "You cannot include Saviour twice in the same class" if @klass.respond_to?(:attached_files)

  @klass.class_attribute :attached_files
  @klass.attached_files = []
  @klass.class_attribute :attached_followers_per_leader
  @klass.attached_followers_per_leader = {}

  klass = @klass
  persistence_klass = @persistence_klass

  @klass.define_singleton_method "attach_file" do |attach_as, *maybe_uploader_klass, **opts, &block|
    klass.attached_files.push(attach_as)
    uploader_klass = maybe_uploader_klass[0]

    if opts[:follow]
      klass.attached_followers_per_leader[opts[:follow]] ||= []
      klass.attached_followers_per_leader[opts[:follow]].push(attach_as)
    end

    if uploader_klass.nil? && block.nil?
      raise ArgumentError, "you must provide either an UploaderClass or a block to define it."
    end

    mod = Module.new do
      define_method(attach_as) do
        instance_variable_get("@__uploader_#{attach_as}") || begin
          uploader_klass = Class.new(Saviour::BaseUploader, &block) if block
          new_file = ::Saviour::File.new(uploader_klass, self, attach_as)

          layer = persistence_klass.new(self)
          new_file.set_path!(layer.read(attach_as)) if layer.persisted?

          instance_variable_set("@__uploader_#{attach_as}", new_file)
        end
      end

      define_method("#{attach_as}=") do |value|
        send(attach_as).assign(value)
      end

      define_method("#{attach_as}_changed?") do
        send(attach_as).changed?
      end
    end

    klass.include mod
  end

  @klass.class_attribute :__saviour_validations

  @klass.define_singleton_method("attach_validation") do |attach_as, method_name = nil, &block|
    klass.__saviour_validations ||= Hash.new { [] }
    klass.__saviour_validations[attach_as] += [method_name || block]
  end
end