Sha256: 5838f74247bd9a8e68a53a90adbf6c57cbdd506adb8d7aed4334fee3c3676a92

Contents?: true

Size: 949 Bytes

Versions: 3

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

# Functions for detecting an enum.
module EnumModule
  def enum?(back_card)
    return false unless back_card.is_a?(Array) && back_card.any?

    return true if ordered?(back_card) || enum_with_header?(back_card)

    back_card.each { |element| return false unless element[/^[-+*]\s.*/] }

    return false unless same_prefix?(back_card)

    true
  end

  def ordered?(back_card)
    back_card.each_with_index do |item, index|
      return false unless item.start_with?("#{index + 1}. ")
    end

    true
  end

  private

  def enum_with_header?(back_card)
    return false if back_card.first[/^[-+*]\s.*/]

    back_card[1..back_card.size].each { |element| return false unless element[/^[-+*]\s.*/] }

    true
  end

  def same_prefix?(back_card)
    prefix = back_card.first[0, 2]
    back_card_rest = back_card - [back_card.first]
    back_card_rest.all? { |list_item| list_item.start_with?(prefix) }
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rast-0.18.0 examples/enum_module.rb
rast-0.15.1 examples/enum_module.rb
rast-0.14.0 examples/enum_module.rb