File: method_builder.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (111 lines) | stat: -rw-r--r-- 2,945 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# frozen_string_literal: true

module Unparser
  module Adamantium
    # Build the memoized method
    class MethodBuilder

      # Raised when the method arity is invalid
      class InvalidArityError < ArgumentError

        # Initialize an invalid arity exception
        #
        # @param [Module] descendant
        # @param [Symbol] method
        # @param [Integer] arity
        #
        # @api private
        def initialize(descendant, method, arity)
          super("Cannot memoize #{descendant}##{method}, its arity is #{arity}")
        end

      end # InvalidArityError

      # Raised when a block is passed to a memoized method
      class BlockNotAllowedError < ArgumentError

        # Initialize a block not allowed exception
        #
        # @param [Module] descendant
        # @param [Symbol] method
        #
        # @api private
        def initialize(descendant, method)
          super("Cannot pass a block to #{descendant}##{method}, it is memoized")
        end

      end # BlockNotAllowedError

      # Initialize an object to build a memoized method
      #
      # @param [Module] descendant
      # @param [Symbol] method_name
      #
      # @return [undefined]
      #
      # @api private
      def initialize(descendant, method_name)
        @descendant          = descendant
        @method_name         = method_name
        @original_visibility = visibility
        @original_method     = @descendant.instance_method(@method_name)
        assert_arity(@original_method.arity)
      end

      # Build a new memoized method
      #
      # @example
      #   method_builder.call  # => creates new method
      #
      # @return [UnboundMethod]
      #
      # @api public
      def call
        remove_original_method
        create_memoized_method
        set_method_visibility
        @original_method
      end

    private

      def assert_arity(arity)
        if arity.nonzero?
          fail InvalidArityError.new(@descendant, @method_name, arity)
        end
      end

      def remove_original_method
        name = @method_name
        @descendant.module_eval { undef_method(name) }
      end

      def create_memoized_method
        name =   @method_name
        method = @original_method
        @descendant.module_eval do
          define_method(name) do |&block|
            fail BlockNotAllowedError.new(self.class, name) if block

            memoized_method_cache.fetch(name) do
              method.bind(self).call.freeze
            end
          end
        end
      end

      def set_method_visibility
        @descendant.__send__(@original_visibility, @method_name)
      end

      def visibility
        if    @descendant.private_method_defined?(@method_name)   then :private
        elsif @descendant.protected_method_defined?(@method_name) then :protected
        else
          :public
        end
      end

    end # MethodBuilder
  end # Adamantium
end # Unparser