Sha256: abac815d90391962a48e73d1268ea78f2fec7c947397816390007c9030e8307c

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'ehbrs_ruby_utils/circular_list_spreader'

::RSpec.describe ::EhbrsRubyUtils::CircularListSpreader do
  let(:node_class) do
    ::Class.new do
      ::Kernel.const_set('NodeClass', self)

      class << self
        def from_file(file)
          from_hash(nil, 'ROOT', ::EacRubyUtils::Yaml.load_file(file))
        end

        def from_hash(parent, label, hash)
          new(parent, label).children_from_hash(hash)
        end
      end

      common_constructor :parent, :label
      attr_reader :children

      def children_from_hash(hash)
        @children = if hash.is_a?(::Hash)
                      hash.map { |k, v| self.class.from_hash(self, k, v) }
                    else
                      false
                    end

        self
      end

      def id
        to_circular_list_spreader_path.join(' | ')
      end

      def leaf?
        !children.is_a?(::Enumerable)
      end

      def recursive_leafs
        if leaf?
          [self]
        else
          children.flat_map(&:recursive_leafs)
        end
      end

      def to_circular_list_spreader_path
        return [] if parent.blank?

        parent.if_present([], &:to_circular_list_spreader_path) + [label]
      end

      def to_s
        label
      end
    end
  end

  include_examples 'source_target_fixtures', __FILE__

  def source_data(source_file)
    described_class.new(node_class.from_file(source_file).recursive_leafs).result.map(&:id)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.22.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.21.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.20.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.19.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.18.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb