module Iteraptor

rubocop:disable Style/VariableNumber rubocop:disable Metrics/ModuleLength

Constants

CADA_PROC

cada

HASH_TO_ARRAY_ERROR_MSG
VERSION

Public Class Methods

attach_to(klazz) click to toggle source
# File lib/iteraptor/greedy.rb, line 2
def attach_to klazz
  klazz.send :include, Iteraptor
end
included(base) click to toggle source
# File lib/iteraptor.rb, line 7
def self.included base
  raise "This module might be included into Enumerables only" unless base.ancestors.include? Enumerable
end

Public Instance Methods

aplanar(**params) { |key, value| ... } click to toggle source

rubocop:enable Style/Alias

# File lib/iteraptor.rb, line 51
def aplanar **params
  return self if empty?
  cada(**params).with_object({}) do |(key, value), acc|
    key = key.join(H.iteraptor_delimiter(params)) if params[:full_parent]
    key = key.to_sym if params[:symbolize_keys]
    acc[key] = value unless value.is_a?(Enumerable)
    yield key, value if block_given?
  end
end
compactar(**params) click to toggle source
# File lib/iteraptor.rb, line 35
def compactar **params
  (is_a?(Array) ? compact : self).
    mapa(yield_all: true) do |_p, (k, v)|
      v.is_a?(Array) ? [k, v.compact] : [k, v]
    end.mapa(**params) do |parent, (k, v)|
      p = parent.split(H.iteraptor_delimiter(params)).last
      p.to_i.to_s != p && v.nil? ? nil : [k, v]
    end.tap do |this|
      break {} if this.empty? && is_a?(Hash)
    end
end
escoger(*filter, **params, &λ) click to toggle source
# File lib/iteraptor.rb, line 30
def escoger *filter, **params, &λ
  return self if empty?
  rechazar_o_escoger true, *filter, **params, &λ
end
Also aliased as: segar
iteraptor() click to toggle source
# File lib/iteraptor.rb, line 11
def iteraptor
  @__iteraptor__ ||= Iteraptor::Delegator.new(self)
end
plana_mapa(**params) { |key, value| ... } click to toggle source
# File lib/iteraptor.rb, line 76
def plana_mapa **params
  return enum_for(:plana_mapa, delimiter: params[:delimiter], **params) unless block_given?
  return self if empty?

  cada(**params).with_object([]) do |(key, value), acc|
    key = key.join(H.iteraptor_delimiter(params)) if params[:full_parent]
    key = key.to_sym if params[:symbolize_keys]
    acc << yield(key, value) unless value.is_a?(Enumerable)
  end
end
rechazar(*filter, **params, &λ) click to toggle source
# File lib/iteraptor.rb, line 25
def rechazar *filter, **params, &λ
  return self if empty?
  rechazar_o_escoger false, *filter, **params, &λ
end
recoger(**params) click to toggle source
# File lib/iteraptor.rb, line 61
def recoger **params
  return self if empty?
  # rubocop:disable Style/MultilineBlockChain
  aplanar(**params).each_with_object(
    Hash.new { |h, k| h[k] = h.clone.clear }
  ) do |(k, v), acc|
    keys = k.to_s.split(H.iteraptor_delimiter(params))
    parent = keys[0..-2].reduce(acc){ |h, kk| h[kk] }
    parent[keys.last] = v
  end.mapa(yield_all: true, **params) do |_parent, (k, v)|
    [k, v]
  end
  # rubocop:enable Style/MultilineBlockChain
end
segar(*filter, **params, &λ)

rubocop:disable Style/Alias

Alias for: escoger

Private Instance Methods

attach_to(klazz) click to toggle source
# File lib/iteraptor/greedy.rb, line 2
def attach_to klazz
  klazz.send :include, Iteraptor
end
cada_in_array(root = nil, parent = nil, **params, &λ) click to toggle source
# File lib/iteraptor.rb, line 98
def cada_in_array root = nil, parent = nil, **params, &λ
  cada_in_array_or_hash true, root, parent, **params, &λ
end
Also aliased as: cada_in_enumerable
cada_in_array_or_hash(in_array, root = nil, parent = nil, **params, &λ) { |p, v| ... } click to toggle source
# File lib/iteraptor.rb, line 107
def cada_in_array_or_hash in_array, root = nil, parent = nil, **params, &λ
  (in_array ? each_with_index : each).each do |k, v|
    k, v = v, k if in_array
    result = H.push_flatten_compact(parent, k)
    result = result.join(H.iteraptor_delimiter(params)) unless params[:full_parent]
    result.tap do |p|
      yield p, v
      CADA_PROC.call(v, root, p, **params, &λ)
    end
  end
end
cada_in_enumerable(root = nil, parent = nil, **params, &λ)
Alias for: cada_in_array
cada_in_hash(root = nil, parent = nil, **params, &λ) click to toggle source
# File lib/iteraptor.rb, line 103
def cada_in_hash root = nil, parent = nil, **params, &λ
  cada_in_array_or_hash false, root, parent, **params, &λ
end
hash_to_hash_or_array() click to toggle source
# File lib/iteraptor.rb, line 190
def hash_to_hash_or_array
  raise NoMethodError, HASH_TO_ARRAY_ERROR_MSG % [inspect, self.class] unless is_a?(Hash)
end
mapa_in_array(root = nil, parent = nil, **params, &λ) { |p, (params ? [idx, e] : e)| ... } click to toggle source
mapa

FIXME what happens if I return nil from mapa in array? Params:

- with_index
- yield_all
- symbolize_keys
# File lib/iteraptor.rb, line 126
def mapa_in_array root = nil, parent = nil, **params, &λ
  map.with_index do |e, idx|
    p = H.push_flatten_compact(parent, idx)
    p = p.join(H.iteraptor_delimiter(params)) unless params[:full_parent]

    yielded =
      if !H.enumerable_parent?(e) || params[:yield_all]
        yield p, (params[:with_index] ? [idx.to_s, e] : e)
      else
        e
      end
    # allow blindly return [k, v] instead of check for an array instance
    #  when block arguments are matched as (k, v)
    yielded = yielded.last || yielded.first if
      yielded.is_a?(Array) && yielded.size == 2 && (yielded.last.nil? || yielded.first == e)

    case yielded
    when Iteraptor then yielded.mapa(root, p, **params, &λ)
    when Enumerable then yielded.map(&λ.curry[p])
    else yielded
    end
  end
end
Also aliased as: mapa_in_enumerable
mapa_in_enumerable(root = nil, parent = nil, **params, &λ)
Alias for: mapa_in_array
mapa_in_hash(root = nil, parent = nil, **params, &λ) { |p, [k, v]| ... } click to toggle source

Params:

- yield_all
- symbolize_keys
# File lib/iteraptor.rb, line 154
def mapa_in_hash root = nil, parent = nil, **params, &λ
  map do |k, v|
    p = H.push_flatten_compact(parent, k)
    p = p.join(H.iteraptor_delimiter(params)) unless params[:full_parent]

    k, v = yield p, [k, v] if !v.is_a?(Enumerable) || params[:yield_all]

    case v
    when Iteraptor then [k, v.mapa(root, p, **params, &λ)]
    when Enumerable then [k, v.map(&λ.curry[p])]
    else k.nil? ? nil : [k, v]
    end
  end.compact.send(:to_hash_or_array, **params)
end
rechazar_o_escoger(method, *filter, **params) { |key, value| ... } click to toggle source

filters

# File lib/iteraptor.rb, line 196
def rechazar_o_escoger method, *filter, **params
  raise ArgumentError, "no filter given in call to #{method ? :escoger : :rechazar}" if filter.empty?

  plough = method ? :none? : :any?
  aplanar(**params).each_with_object({}) do |(key, value), acc|
    to_match = key.to_s.split(H.iteraptor_delimiter(params)) unless params[:full_parent]
    to_match = to_match.flat_map { |k| [k.to_s, k.to_s.to_sym] } if params[:soft_keys]

    next if filter.public_send(plough, &->(f){ to_match.any?(&f.method(:===)) })

    value = yield key, value if block_given?
    acc[key] = value
  end.recoger(**params)
end
to_hash_or_array(**params) click to toggle source

helpers

# File lib/iteraptor.rb, line 172
def to_hash_or_array **params
  # rubocop:disable Style/MultilineTernaryOperator
  # rubocop:disable Style/RescueModifier
  receiver =
    is_a?(Array) &&
      all? { |e| e.is_a?(Enumerable) && e.size == 2 } &&
      map(&:first).uniq.size == size ? (to_h rescue self) : self
  # rubocop:enable Style/RescueModifier

  return receiver unless receiver.is_a?(Hash)
  return receiver.values if receiver.keys.each_with_index.all? { |key, idx| key == idx.to_s }
  return receiver unless params[:symbolize_keys]

  receiver.map { |k, v| [H.safe_symbolize(k), v] }.to_h
  # rubocop:enable Style/MultilineTernaryOperator
end