Sha256: 53d840675a9edad1547838bdd6e5b8801576b3df3f485f49c56e778b9349b9d2

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# typed: false
# frozen_string_literal: true

module Enumerable
  def where(options = {})
    obj = first
    list = if obj.is_a?(::Hash)
             self
           else
             []
           end
    Workato::Extension::Array::ArrayWhere.new(list, options || {})
  end

  def format_map(format)
    if format.blank?
      return self
    end

    map do |val|
      format % (Array.wrap(val).map { |v| v.is_a?(HashWithIndifferentAccess) ? v.symbolize_keys : v })
    end
  end

  def smart_join(separator)
    transform_select do |val|
      val = val.strip if val.is_a?(::String)
      val.presence
    end.join(separator)
  end

  def pluck(*keys)
    if keys.many?
      map { |element| keys.map { |key| element.respond_to?(:dig) ? element.dig(*key) : element[key] } }
    else
      map { |element| element.respond_to?(:dig) ? element.dig(*keys.first) : element[keys.first] }
    end
  end

  def transform_find(&block)
    each do |*items|
      result = block.call(*items)
      return result if result
    end
    nil
  end

  private

  def transform_select(&block)
    map do |*items|
      result = block.call(*items)
      result || nil
    end.compact
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workato-connector-sdk-1.3.1 lib/workato/extension/enumerable.rb
workato-connector-sdk-1.3.0 lib/workato/extension/enumerable.rb