Sha256: b54c50e3758a1be16776e81523c2ef85ad1624ef91fbac2a953922c1ea460efa

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

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 do |key, value|
            unless value.is_a?(Structure) ||
                   (defined?(::RSpec::Mocks::Double) &&
                    value.is_a?(::RSpec::Mocks::Double))
              value.freeze
            end

            instance_variable_set :"@#{key}", value
          end
        end

        attribute_names.each do |name|
          module_eval <<-CODE, __FILE__, __LINE__ + 1
            def __#{name}__
              @#{name}
            end
          CODE
          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.1.0 lib/structure/double.rb