Sha256: f818eedec33dede04712c5281afe779daa0251f668ba1d7b72d49352fbf075d3

Contents?: true

Size: 839 Bytes

Versions: 11

Compression:

Stored size: 839 Bytes

Contents

module NxtSupport
  module Services
    module Base
      module ClassMethods
        def class_interface(config = :call)
          if config.is_a?(Symbol)
            define_singleton_method config do |*args, **opts|
              build_instance(*args, **opts).send(config)
            end
          else
            raise ArgumentError, "Wrong configuration. Please use 'class_interface :your_method_name'"
          end
        end

        private

        # Ruby <2.7-specific check. If the gem is updated to 2.7, `**opts` will work.
        def build_instance(*args, **opts)
          if opts.empty?
            new(*args, **{})
          else
            new(*args, **opts)
          end
        end
      end

      def self.included(base)
        base.extend(ClassMethods)
        base.class_interface
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
nxt_support-0.6.0 lib/nxt_support/services/base.rb
nxt_support-0.5.0 lib/nxt_support/services/base.rb
nxt_support-0.4.3 lib/nxt_support/services/base.rb
nxt_support-0.4.2 lib/nxt_support/services/base.rb
nxt_support-0.4.1 lib/nxt_support/services/base.rb
nxt_support-0.4.0 lib/nxt_support/services/base.rb
nxt_support-0.3.0 lib/nxt_support/services/base.rb
nxt_support-0.2.1 lib/nxt_support/services/base.rb
nxt_support-0.2.0 lib/nxt_support/services/base.rb
nxt_support-0.1.16 lib/nxt_support/services/base.rb
nxt_support-0.1.15 lib/nxt_support/services/base.rb