Sha256: fd0dc8f9693d5f3951475347798a1dae6ece7d9e49f3b6bb296895700864e0a1

Contents?: true

Size: 1.47 KB

Versions: 19

Compression:

Stored size: 1.47 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

19 entries across 18 versions & 3 rubygems

Version Path
httparty-0.16.2 lib/httparty/module_inheritable_attributes.rb
httparty-0.16.1 lib/httparty/module_inheritable_attributes.rb
httparty-0.16.0 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.7 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.6 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.5 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.4 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.3 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.2 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.1 lib/httparty/module_inheritable_attributes.rb
httparty-0.15.0 lib/httparty/module_inheritable_attributes.rb
httparty-0.14.0 lib/httparty/module_inheritable_attributes.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/2.0.0/gems/httparty-0.13.7/lib/httparty/module_inheritable_attributes.rb
simplenet-client-0.2.0 ./vendor/bundle/ruby/1.9.1/gems/httparty-0.13.7/lib/httparty/module_inheritable_attributes.rb
httparty-0.13.7 lib/httparty/module_inheritable_attributes.rb
httparty-0.13.6 lib/httparty/module_inheritable_attributes.rb
httpserious-0.13.5.lstoll1 lib/httparty/module_inheritable_attributes.rb
httparty-0.13.5 lib/httparty/module_inheritable_attributes.rb
httparty-0.13.4 lib/httparty/module_inheritable_attributes.rb