File: singleton.rb

package info (click to toggle)
ruby-naught 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: ruby: 658; makefile: 6
file content (25 lines) | stat: -rw-r--r-- 672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
require "singleton"
require "naught/null_class_builder/command"

module Naught
  class NullClassBuilder
    module Commands
      # Turns the null class into a Singleton
      #
      # @api private
      class Singleton < Command
        # Install Singleton behavior on the null class
        # @return [void]
        # @api private
        def call
          defer_class do |klass|
            klass.include(::Singleton)
            klass.singleton_class.undef_method(:get)
            klass.define_singleton_method(:get) { |*| instance }
            %i[dup clone].each { |name| klass.define_method(name) { self } }
          end
        end
      end
    end
  end
end