Sha256: 4bcae703855a7901a7d9a8c3e1863ca820cf35337b55a3826390f52f9697c3a1

Contents?: true

Size: 1.45 KB

Versions: 57

Compression:

Stored size: 1.45 KB

Contents

module HTTParty
  module ModuleInheritableAttributes #:nodoc:
    def self.included(base)
      base.extend(ClassMethods)
    end

    # borrowed from Rails 3.2 ActiveSupport
    def self.hash_deep_dup(hash)
      duplicate = hash.dup

      duplicate.each_pair do |key, value|
        duplicate[key] = if value.is_a?(Hash)
          hash_deep_dup(value)
        elsif value.is_a?(Proc)
          duplicate[key] = value.dup
        else
          value
        end
      end

      duplicate
    end

    module ClassMethods #:nodoc:
      def mattr_inheritable(*args)
        @mattr_inheritable_attrs ||= [:mattr_inheritable_attrs]
        @mattr_inheritable_attrs += args

        args.each do |arg|
          module_eval %(class << self; attr_accessor :#{arg} end)
        end

        @mattr_inheritable_attrs
      end

      def inherited(subclass)
        super
        @mattr_inheritable_attrs.each do |inheritable_attribute|
          ivar = "@#{inheritable_attribute}"
          subclass.instance_variable_set(ivar, instance_variable_get(ivar).clone)

          if instance_variable_get(ivar).respond_to?(:merge)
            method = <<-EOM
              def self.#{inheritable_attribute}
                duplicate = ModuleInheritableAttributes.hash_deep_dup(#{ivar})
                #{ivar} = superclass.#{inheritable_attribute}.merge(duplicate)
              end
            EOM

            subclass.class_eval method
          end
        end
      end
    end
  end
end

Version data entries

57 entries across 56 versions & 8 rubygems

Version Path
alloy_sdk-0.1.0 vendor/bundle/ruby/2.6.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
httsoiree-0.13.3 lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.9 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
httparty-0.13.3 lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.8 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.7 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
httparty-0.13.2 lib/httparty/module_inheritable_attributes.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.6 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.5 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.4 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.3 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.2 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
allegro_api_client-0.0.1 gems/ruby/2.1.0/gems/httparty-0.13.1/lib/httparty/module_inheritable_attributes.rb
httsoiree-0.13.1.1 lib/httparty/module_inheritable_attributes.rb
httsoiree-0.13.1 lib/httparty/module_inheritable_attributes.rb
httparty-enterprise-edition-0.13.1.1 lib/httparty/module_inheritable_attributes.rb
httparty-enterprise-edition-0.13.1 lib/httparty/module_inheritable_attributes.rb
httparty-0.13.1 lib/httparty/module_inheritable_attributes.rb