Sha256: 7aba8be44e9b593670a550b7326d6be9d10ed7a2432e3987b3537b1aeba3cbbf

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 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|
            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 "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.3 lib/structure/double.rb