Skip to content

Commit

Permalink
#20 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 5, 2024
1 parent eaa511e commit 15705d3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Expand Up @@ -18,3 +18,9 @@ Layout/ElseAlignment:
Enabled: false
Layout/IndentationWidth:
Enabled: false
Metrics/AbcSize:
Max: 30
Metrics/PerceivedComplexity:
Max: 15
Metrics/CyclomaticComplexity:
Max: 15
17 changes: 13 additions & 4 deletions lib/obk.rb
Expand Up @@ -40,12 +40,21 @@ def initialize(origin, pause: 1000)
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)
mtd = args.shift
result = if @origin.respond_to?(mtd)
params = @origin.method(mtd).parameters
reqs = params.count { |p| p[0] == :req }
if params.any? { |p| p[0] == :key } && args.size > reqs
@origin.__send__(mtd, *args[0...-1], **args.last) do |*a|
yield(*a) if block_given?
end
else
@origin.__send__(mtd, *args) do |*a|
yield(*a) if block_given?
end
end
else
@origin.__send__(*args)
super
end
@latest = Time.now
result
Expand Down
25 changes: 23 additions & 2 deletions test/test_obk.rb
Expand Up @@ -36,11 +36,32 @@ def test_simple
def obj.read(foo)
foo
end
obj = Obk.new(obj, pause: 1000)
obj = Obk.new(obj, pause: 100)
start = Time.now
assert_equal(42, obj.read(42))
assert_equal(42, obj.read(42))
stop = Time.now
assert(stop - start > 1)
assert(stop - start > 0.1)
end

def test_works_with_optional_arguments
obj = Object.new
def obj.foo(first, _second, ext1: 'a', ext2: 'b')
first + ext1 + ext2
end
synced = Obk.new(obj, pause: 10)
assert_equal('.xy', synced.foo('.', {}, ext1: 'x', ext2: 'y'))
assert_equal('fzb', synced.foo('f', {}, ext1: 'z'))
assert_equal('-ab', synced.foo('-', {}))
end

def test_works_with_default_value
obj = Object.new
def obj.foo(first, second = 42)
first + second
end
synced = Obk.new(obj, pause: 10)
assert_equal(15, synced.foo(7, 8))
assert_equal(43, synced.foo(1))
end
end

0 comments on commit 15705d3

Please sign in to comment.