Sha256: 20f8727efc29cebbe85c16d4064e2e7f9fad99a24d3476f2aee5f2241b697cea

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Paperclip; module Ext
  module Class
    def self.inheritable_attributes(klass)
      unless klass.instance_variable_defined?(IVar)
        klass.instance_variable_set(IVar, EMPTY_INHERITABLE_ATTRIBUTES)
      end
      klass.instance_variable_get(IVar)
    end

    def self.write_inheritable_attribute(klass, key, value)
      if inheritable_attributes(klass).equal?(EMPTY_INHERITABLE_ATTRIBUTES)
        klass.instance_variable_set(IVar, {})
      end
      inheritable_attributes(klass)[key] = value
    end

    def self.read_inheritable_attribute(klass, key)
      inheritable_attributes(klass)[key]
    end

    def self.reset_inheritable_attributes(klass)
      klass.instance_variable_set(IVar, EMPTY_INHERITABLE_ATTRIBUTES)
    end

    module Hook
      def inherited(base)
        super

        attributes = ::Paperclip::Ext::Class.inheritable_attributes(self)
        new_attributes =
          if attributes.equal?(::Paperclip::Ext::Class::EMPTY_INHERITABLE_ATTRIBUTES)
            ::Paperclip::Ext::Class::EMPTY_INHERITABLE_ATTRIBUTES
          else
            attributes.inject({}) do |memo, (key, value)|
              memo[key] = ::Paperclip::Ext.try_dup(value.dup)
              memo
            end
          end

        base.instance_variable_set(::Paperclip::Ext::Class::IVar, new_attributes)
      end
    end

  private
    IVar = "@_C2DE8FA4_FDA9_45A9_8952_0AEFB571DCC1_inheritable_attributes"

    # Prevent this constant from being created multiple times
    EMPTY_INHERITABLE_ATTRIBUTES = {}.freeze
  end
end; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-paperclip-2.5.0 lib/dm-paperclip/ext/class.rb