Sha256: 81c111b113d46f39e73cd392e280e998bb20b8c4a14fa99d5107db9b73efaab7

Contents?: true

Size: 1.48 KB

Versions: 15

Compression:

Stored size: 1.48 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

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

            class_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{meth}!(*args)
                if args.last.is_a? Hash
                  args.last.merge!(bang: true)
                else
                  args.push({ bang: true })
                end
                #{meth}(*args)
              end
            RUBY
          end
        end
        # rubocop:enable Metrics/MethodLength
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pricehubble-1.3.0 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.5 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.4 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.3 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.2 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.1 lib/price_hubble/utils/bangers.rb
pricehubble-1.2.0 lib/price_hubble/utils/bangers.rb
pricehubble-1.1.0 lib/pricehubble/utils/bangers.rb
pricehubble-1.0.0 lib/pricehubble/utils/bangers.rb
pricehubble-0.4.2 lib/pricehubble/utils/bangers.rb
pricehubble-0.4.1 lib/pricehubble/utils/bangers.rb
pricehubble-0.4.0 lib/pricehubble/utils/bangers.rb
pricehubble-0.3.0 lib/pricehubble/utils/bangers.rb
pricehubble-0.2.0 lib/pricehubble/utils/bangers.rb
pricehubble-0.1.0 lib/pricehubble/utils/bangers.rb