Module: PriceHubble::Utils::Bangers

Extended by:
ActiveSupport::Concern
Included in:
BaseEntity, BaseEntity, Client::Base
Defined in:
lib/pricehubble/utils/bangers.rb

Overview

Generate bang variants of methods which use the Decision flow control.

Class Method Summary collapse

Class Method Details

.bangers(*methods) ⇒ Object

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.

rubocop:disable Metrics/MethodLength because the method template

is better inlined

Parameters:

  • methods (Array<Symbol>)

    the source method names

Raises:

  • (NoMethodError)

    when a source method is not defined

  • (ArgumentError)

    when a source method does not accept arguments



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pricehubble/utils/bangers.rb', line 20

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