Sha256: c037d7e8894161c0011ba37ef8e5bb842c7f3a5a597ed23622356409565cf638

Contents?: true

Size: 841 Bytes

Versions: 4

Compression:

Stored size: 841 Bytes

Contents

# frozen_string_literal: true

require 'chamber/refinements/deep_dup'
require 'chamber/refinements/hash'

module  Chamber
module  Filters
class   NamespaceFilter
  using ::Chamber::Refinements::DeepDup
  using ::Chamber::Refinements::Hash

  attr_accessor :data,
                :namespaces

  def self.execute(**args)
    new(**args).__send__(:execute)
  end

  def initialize(data:, namespaces:, **_args)
    self.data       = data.deep_dup
    self.namespaces = namespaces
  end

  protected

  def execute
    return data unless data_is_namespaced?

    namespaces.each_with_object({}) do |namespace, filtered_data|
      filtered_data.deep_merge!(data[namespace]) if data[namespace]
    end
  end

  private

  def data_is_namespaced?
    @data_is_namespaced ||= data.keys.any? { |key| namespaces.include?(key.to_s) }
  end
end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chamber-3.1.1 lib/chamber/filters/namespace_filter.rb
chamber-3.1.0 lib/chamber/filters/namespace_filter.rb
chamber-3.0.1 lib/chamber/filters/namespace_filter.rb
chamber-3.0.0 lib/chamber/filters/namespace_filter.rb