Sha256: 9d6cd238deefe4da480dab4ae8496edceaad40817323919d27e41d00cdcdbdc0

Contents?: true

Size: 1.11 KB

Versions: 6

Compression:

Stored size: 1.11 KB

Contents

# With thanks to HTTParty: https://github.com/jnunemaker/httparty/blob/master/lib/httparty/module_inheritable_attributes.rb
module ElsToken
  module ModuleInheritableAttributes
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      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}
                #{ivar} = superclass.#{inheritable_attribute}.merge #{ivar}
              end
            EOM
            subclass.class_eval method
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
els_token-1.2.4 lib/els_token/module_inheritable_attributes.rb
els_token-1.2.3 lib/els_token/module_inheritable_attributes.rb
els_token-1.2.1 lib/els_token/module_inheritable_attributes.rb
els_token-1.2.0 lib/els_token/module_inheritable_attributes.rb
els_token-1.0.1 lib/els_token/module_inheritable_attributes.rb
els_token-1.0.0 lib/els_token/module_inheritable_attributes.rb