Sha256: cb43111b6fcbad4d96339261ca4db6273a12f377b9eb98ace35ac32add5c5f6e

Contents?: true

Size: 731 Bytes

Versions: 1

Compression:

Stored size: 731 Bytes

Contents

module Structure
  module ClassMethods
    def double
      klass = Class.new(self)

      (
        private_instance_methods(false) +
        protected_instance_methods(false) -
        [:initialize]
      ).each do |name|
        klass.send(:undef_method, name)
      end

      klass.module_eval do
        def initialize(data = {})
          data.each { |key, value|
            instance_variable_set(:"@#{key}", value)
          }
        end

        attribute_names.each do |name|
          module_eval "def _#{name}; @#{name}; end"
          private "_#{name}"
        end

        module_eval(&Proc.new) if block_given?
      end

      class << klass
        undef_method :double
      end

      klass
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
structure-1.0.1 lib/structure/double.rb