Sha256: 57bed5c3bf2ee1d5660b899e7beac715db4e8735adf3a64399147f171821fbe2

Contents?: true

Size: 806 Bytes

Versions: 1

Compression:

Stored size: 806 Bytes

Contents

module Structure
  # ClassMethods
  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 do |key, value|
            value.freeze unless value.is_a?(Structure)
            instance_variable_set(:"@#{key}", value)
          end
        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.2 lib/structure/double.rb