Sha256: cc6863650baec2661ad8717e320201e7eef08fe009fd929730513f6672bdd468

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module QueueClassicPlus
  # From https://github.com/apotonick/uber/blob/master/lib/uber/inheritable_attr.rb which is MIT license
  module InheritableAttribute
    def inheritable_attr(name)
      instance_eval %Q{
        def #{name}=(v)
          @#{name} = v
        end
        def #{name}
          return @#{name} if instance_variable_defined?(:@#{name})
          @#{name} = InheritableAttribute.inherit_for(self, :#{name})
        end
      }
    end

    def self.inherit_for(klass, name)
      return unless klass.superclass.respond_to?(name)

      value = klass.superclass.send(name) # could be nil.
      Clone.(value) # this could be dynamic, allowing other inheritance strategies.
    end

    class Clone
      # The second argument allows injecting more types.
      def self.call(value, uncloneable=uncloneable())
        uncloneable.each { |klass| return value if value.kind_of?(klass) }
        value.clone
      end

      def self.uncloneable
        [Symbol, TrueClass, FalseClass, NilClass]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
queue_classic_plus-1.0.0.alpha2 lib/queue_classic_plus/inheritable_attr.rb