Sha256: 52d38db2b0014ecf52c0c1ab716b850e842a2c42e6429d0261a152097d99857f

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

require 'singleton'

module Paperclip
  class AttachmentRegistry
    include Singleton

    def self.register(klass, attachment_name, attachment_options)
      instance.register(klass, attachment_name, attachment_options)
    end

    def self.clear
      instance.clear
    end

    def self.names_for(klass)
      instance.names_for(klass)
    end

    def self.each_definition(&block)
      instance.each_definition(&block)
    end

    def self.definitions_for(klass)
      instance.definitions_for(klass)
    end

    def initialize
      clear
    end

    def register(klass, attachment_name, attachment_options)
      @attachments ||= {}
      @attachments[klass] ||= {}
      @attachments[klass][attachment_name] = attachment_options
    end

    def clear
      @attachments = Hash.new { |h,k| h[k] = {} }
    end

    def names_for(klass)
      @attachments[klass].keys
    end

    def each_definition
      @attachments.each do |klass, attachments|
        attachments.each do |name, options|
          yield klass, name, options
        end
      end
    end

    def definitions_for(klass)
      klass.ancestors.each_with_object({}) do |ancestor, inherited_definitions|
        inherited_definitions.deep_merge! @attachments[ancestor]
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
kt-paperclip-4.4.0 lib/paperclip/attachment_registry.rb
paperclip-4.3.7 lib/paperclip/attachment_registry.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/lib/paperclip/attachment_registry.rb
paperclip-4.3.6 lib/paperclip/attachment_registry.rb
paperclip-4.3.5 lib/paperclip/attachment_registry.rb
paperclip-4.3.4 lib/paperclip/attachment_registry.rb
paperclip-4.3.3 lib/paperclip/attachment_registry.rb
paperclip-4.3.2 lib/paperclip/attachment_registry.rb
paperclip-4.3.1 lib/paperclip/attachment_registry.rb
paperclip-4.3.0 lib/paperclip/attachment_registry.rb