Class: Obk

Inherits:
Object
  • Object
show all
Defined in:
lib/obk.rb

Overview

Obk is a decorator that adds delays between object method calls.

For more information read README file.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2021-2022 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(origin, pause: 1000) ⇒ Obk

Returns a new instance of Obk.



34
35
36
37
38
# File 'lib/obk.rb', line 34

def initialize(origin, pause: 1000)
  @origin = origin
  @pause = pause / 1000.0
  @latest = Time.now
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/obk.rb', line 40

def method_missing(*args)
  left = @pause - (Time.now - @latest)
  sleep left if left.positive?
  result = if block_given?
    @origin.__send__(*args) do |*a|
      yield(*a)
    end
  else
    @origin.__send__(*args)
  end
  @latest = Time.now
  result
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/obk.rb', line 54

def respond_to?(method, include_private = false)
  @origin.respond_to?(method, include_private)
end

#respond_to_missing?(_method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/obk.rb', line 58

def respond_to_missing?(_method, _include_private = false)
  true
end