Sha256: e30dc5ab0f5dd5fca620e57bc517416ec632b884aeac3a5c4e19d8a5cdc36202

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module PriceHubble
  module Utils
    # Generate bang variants of methods which use the +Decision+ flow control.
    module Bangers
      extend ActiveSupport::Concern

      class_methods do
        # Generate bang variants for the given methods.
        # Be sure to use the +bangers+ class method AFTER all method
        # definitions, otherwise it will raise errors about missing methods.
        #
        # @param methods [Array<Symbol>] the source method names
        # @raise [NoMethodError] when a source method is not defined
        # @raise [ArgumentError] when a source method does not accept arguments
        #
        # rubocop:disable Metrics/MethodLength because the method template
        #   is better inlined
        def bangers(*methods)
          methods.each do |meth|
            raise NoMethodError, "#{self}##{meth} does not exit" \
              unless instance_methods(false).include? meth

            if instance_method(meth).arity.zero?
              raise ArgumentError,
                    "#{self}##{meth} does not accept arguments"
            end

            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{meth}!(*args, **kwargs, &block)
                #{meth}(*args, **kwargs, bang: true, &block)
              end
            RUBY
          end
        end
        # rubocop:enable Metrics/MethodLength
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pricehubble-1.6.0 lib/price_hubble/utils/bangers.rb
pricehubble-1.5.1 lib/price_hubble/utils/bangers.rb
pricehubble-1.5.0 lib/price_hubble/utils/bangers.rb
pricehubble-1.4.0 lib/price_hubble/utils/bangers.rb