Module: CountryCodes::Internal::Helpers

Included in:
Country, LookupTable
Defined in:
lib/country_codes/internal/helpers.rb

Overview

Note:

This code is not supposed to be used externally!

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(obj) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/country_codes/internal/helpers.rb', line 15

def self.included(obj)
  obj.class_eval do
    # Defines a method(s) that redirect calls to an underlying object (receiver).
    # Allows passing a list of interceptors that will be applied sequentially
    # with the result of redirected call as the first (and only) argument.
    # An interceptor can be either a Proc (the proc itself will be `#call`-ed),
    # or a Symbol as a name of an instance method to be used.
    #
    # @param [Array<Symbol>] methods Name of methods to be defined
    # @param [Object] to Name of an instance method used to retrieve the receiver
    # @param [Array<Proc, Symbol>] interceptors A list of interceptors.
    def self.delegate(*methods, to:, interceptors: [])
      unless methods.reject { |m| m.is_a? Symbol }.empty?
        raise ArgumentError, 'expected method reference to be a Symbol!'
      end

      interceptors = [interceptors] unless interceptors.is_a?(Array)

      methods.each do |method|
        define_method(method) do |*args|
          result = send(to).send(method, *args)

          interceptors.reduce(result) do |memo, interceptor|
            case interceptor
            when Proc
              interceptor.call(memo)
            when Symbol
              send(interceptor, memo)
            else
              memo
            end
          end
        end
      end
    end
  end
end

Instance Method Details

#pack_seq(enumerable) ⇒ JavaLangObject

Converts a given enumerable to a ‘scala.collection.Seq` Java object

Parameters:

  • enumerable (Enumerable)

    An enumerable to wrap

Returns:

  • (JavaLangObject)

    A wrapped Seq object



78
79
80
81
82
# File 'lib/country_codes/internal/helpers.rb', line 78

def pack_seq(enumerable)
  list_iter = ArrayList.new(enumerable.to_a).iterator

  JavaConverters.as_scala_iterator_converter(list_iter).as_scala.to_seq
end

#unpack_seq(seq) ⇒ JavaLangObject

Converts a ‘scala.collection.Seq` Java object to a Ruby array

Parameters:

  • seq (JavaLangObject)

    An object to convert

Returns:

  • (JavaLangObject)

    Extracted array



70
71
72
# File 'lib/country_codes/internal/helpers.rb', line 70

def unpack_seq(seq)
  JavaConverters.seq_as_java_list(seq).to_a
end

#unpack_some(some) ⇒ Object

Unpacks a scala.Some Java object to a value or ‘nil` (if an object is empty).

Parameters:

  • some (JavaLangObject)

    A ‘scala.Some` object to unpack

Returns:

  • (Object)

    A value contained in the object, or nil, if the object is empty



58
59
60
61
62
63
64
# File 'lib/country_codes/internal/helpers.rb', line 58

def unpack_some(some)
  if some.empty?
    nil
  else
    some.get
  end
end