· 6 years ago · Sep 26, 2019, 11:40 PM
1module REST
2 def self.OAuth(client)
3 REST::OAuth.new(client)
4 end
5
6 class OAuth < Module
7 def initialize(client)
8 @client = client
9 freeze
10 end
11
12 private
13
14 def included(descendant)
15 super
16 descendant.send(:include, Methods)
17 end
18
19 def define_methods
20 define_cmp_method
21 define_hash_method
22 define_inspect_method if inspect
23 end
24
25 # Define an #cmp? method based on the instance's values identified by #keys
26 #
27 # @return [undefined]
28 #
29 # @api private
30 def define_cmp_method
31 keys = @keys
32 define_method(:cmp?) do |comparator, other|
33 keys.all? do |key|
34 __send__(key).public_send(comparator, other.__send__(key))
35 end
36 end
37 private :cmp?
38 end
39
40 # Define a #hash method based on the instance's values identified by #keys
41 #
42 # @return [undefined]
43 #
44 # @api private
45 def define_hash_method
46 keys = @keys
47 define_method(:hash) do | |
48 keys.map(&method(:send)).push(self.class).hash
49 end
50 end
51
52 # Define an inspect method that reports the values of the instance's keys
53 #
54 # @return [undefined]
55 #
56 # @api private
57 def define_inspect_method
58 keys = @keys
59 define_method(:inspect) do | |
60 klass = self.class
61 name = klass.name || klass.inspect
62 "#<#{name}#{keys.map { |key| " #{key}=#{__send__(key).inspect}" }.join}>"
63 end
64 end
65
66 # The comparison methods
67 module Methods
68 # Compare the object with other object for equality
69 #
70 # @example
71 # object.eql?(other) # => true or false
72 #
73 # @param [Object] other
74 # the other object to compare with
75 #
76 # @return [Boolean]
77 #
78 # @api public
79 def eql?(other)
80 instance_of?(other.class) && cmp?(__method__, other)
81 end
82
83 # Compare the object with other object for equivalency
84 #
85 # @example
86 # object == other # => true or false
87 #
88 # @param [Object] other
89 # the other object to compare with
90 #
91 # @return [Boolean]
92 #
93 # @api public
94 def ==(other)
95 other.is_a?(self.class) && cmp?(__method__, other)
96 end
97 end # module Methods
98 end # class Equalizer
99end