Sha256: a6f3418f05290e6d03dc8af33728c22d6bcd9748f9dc16bdec2c9997550908b6

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'thread'

module Protoable
  module InheritableClassInstanceVariables
    def self.included(klass)
      Thread.exclusive do
        klass.extend(Protoable::InheritableClassInstanceVariables::ClassMethods)

        klass.class_eval do
          @_inheritable_class_instance_variables = [ :_inheritable_class_instance_variables ]
        end
      end
    end

    module ClassMethods
      def inheritable_attributes(*args)
        Thread.exclusive do
          args.flatten.compact.uniq.each do |class_instance_variable|
            unless @_inheritable_class_instance_variables.include?(class_instance_variable)
              @_inheritable_class_instance_variables << class_instance_variable
            end
          end

          @_inheritable_class_instance_variables.each do |attr_symbol|
            unless self.respond_to?("#{attr_symbol}")
              class_eval %Q{
                class << self; attr_reader :#{attr_symbol}; end
              }
            end

            unless self.respond_to?("#{attr_symbol}=")
              class_eval %Q{
                class << self; attr_writer :#{attr_symbol}; end
              }
            end
          end

          @_inheritable_class_instance_variables
        end
      end

      def inherited(klass)
        super # ActiveRecord needs the inherited hook to setup fields

        Thread.exclusive do
          @_inheritable_class_instance_variables.each do |attribute|
            attr_sym = :"@#{attribute}"
            klass.instance_variable_set(attr_sym, self.instance_variable_get(attr_sym))
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
protobuf-activerecord-1.0.4 lib/protoable/inheritable_class_instance_variables.rb
protobuf-activerecord-1.0.3 lib/protoable/inheritable_class_instance_variables.rb
protobuf-activerecord-1.0.2 lib/protoable/inheritable_class_instance_variables.rb
protobuf-activerecord-1.0.1 lib/protoable/inheritable_class_instance_variables.rb