Sha256: d2ad43d8b9fce86b7178107fdf61ad9213bbd485b67cec88d05b53569bf7451d

Contents?: true

Size: 1.46 KB

Versions: 14

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
      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

        def name
          'NodeClass'
        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

14 entries across 14 versions & 1 rubygems

Version Path
ehbrs_ruby_utils-0.33.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.32.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.31.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.30.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.29.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.28.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.27.1 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.27.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.26.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.25.1 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.25.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.24.1 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.24.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb
ehbrs_ruby_utils-0.23.0 spec/lib/ehbrs_ruby_utils/circular_list_spreader_spec.rb