Sha256: 2bf4c8fe06cee67c58d6b1f5e2ffcf62bc9e9eaff8b31e263c4d4d975d795cac

Contents?: true

Size: 963 Bytes

Versions: 12

Compression:

Stored size: 963 Bytes

Contents

module Uber
  module InheritableAttr
    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

  InheritableAttribute = InheritableAttr
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
garcun-0.1.1 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.1.0 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.9 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.8 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.7 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.6 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.5 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.4 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.3 lib/garcon/utility/uber/inheritable_attr.rb
garcun-0.0.2 lib/garcon/utility/uber/inheritable_attr.rb
uber-0.0.13 lib/uber/inheritable_attr.rb
uber-0.0.12 lib/uber/inheritable_attr.rb