File: lazy_object.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (43 lines) | stat: -rw-r--r-- 896 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

# Based on https://github.com/HornsAndHooves/lazy_object

module Grape
  module Util
    class LazyObject < BasicObject
      attr_reader :callable

      def initialize(&callable)
        @callable = callable
      end

      def __target_object__
        @__target_object__ ||= callable.call
      end

      def ==(other)
        __target_object__ == other
      end

      def !=(other)
        __target_object__ != other
      end

      def !
        !__target_object__
      end

      def method_missing(method_name, *args, &block)
        if __target_object__.respond_to?(method_name)
          __target_object__.send(method_name, *args, &block)
        else
          super
        end
      end

      def respond_to_missing?(method_name, include_priv = false)
        __target_object__.respond_to?(method_name, include_priv)
      end
    end
  end
end