Skip to content

Commit

Permalink
Use conditional method definition to support 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeepers committed Dec 1, 2023
1 parent b3f5c35 commit 0245c2c
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/flipper/adapter_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ def initialize(&block)
block.arity == 0 ? instance_eval(&block) : block.call(self) if block
end

def use(klass, *args, &block)
@stack.push ->(adapter) { klass.new(adapter, *args, &block) }
if RUBY_VERSION >= '2.7'
def use(klass, ...)
@stack.push ->(adapter) { klass.new(adapter, ...) }
end
else
def use(klass, *args, &block)
@stack.push ->(adapter) { klass.new(adapter, *args, &block) }
end
end

def store(adapter, *args, &block)
@store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(*args, &block) }
if RUBY_VERSION >= '2.7'
def store(adapter, ...)
@store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(...) }
end
else
def store(adapter, *args, &block)
@store = adapter.respond_to?(:call) ? adapter : -> { adapter.new(*args, &block) }
end
end

def to_adapter
@stack.reverse.inject(@store.call) { |adapter, wrapper| wrapper.call(adapter) }
end

# Properly pass kwargs to `use` and `store` methods.
# Replace with `...` once support for Ruby 2.6 and 2.7 are dropped
if respond_to?(:ruby2_keywords, true)
ruby2_keywords :use
ruby2_keywords :store
end
end
end

0 comments on commit 0245c2c

Please sign in to comment.