File: define_implicit_conversions.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 (29 lines) | stat: -rw-r--r-- 825 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
26
27
28
29
require "naught/null_class_builder/command"

module Naught
  class NullClassBuilder
    module Commands
      # Adds implicit conversion methods to the null class
      #
      # @api private
      class DefineImplicitConversions < Command
        EMPTY_ARRAY = [] #: Array[untyped]
        EMPTY_HASH = {} #: Hash[untyped, untyped]
        RETURN_VALUES = {
          to_ary: EMPTY_ARRAY.freeze,
          to_hash: EMPTY_HASH.freeze,
          to_int: 0,
          to_str: "".freeze
        }.freeze
        private_constant :EMPTY_ARRAY, :EMPTY_HASH, :RETURN_VALUES

        # Install implicit conversion methods
        # @return [void]
        # @api private
        def call
          defer { |subject| RETURN_VALUES.each { |name, value| subject.define_method(name) { value } } }
        end
      end
    end
  end
end