Module: Shrine::Plugins::Hanami::AttachmentMethods

Defined in:
lib/shrine/plugins/hanami.rb

Instance Method Summary collapse

Instance Method Details

#initialize(name, **options) ⇒ Object



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
# File 'lib/shrine/plugins/hanami.rb', line 9

def initialize(name, **options)
  super

  module_eval <<-RUBY, __FILE__, __LINE__ + 1
    module EntitySupport
      attr_reader :attributes
      def initialize(attributes)
        attachment = attributes[:#{name}]
        @_#{name} = attachment
        self.#{name}_attacher
        super(attributes)
      end

      def #{name}_data=(data)
        @#{name}_data = data
      end

      def #{name}_data
        super || @#{name}_data
      end

      def #{name}
        @_#{name} || super
      end

      def attributes
        @_#{name} ? super.merge(#{name}: @_#{name}) : super
      end
    end

    prepend EntitySupport
  RUBY
end