Sha256: f2dd95c62769328507d78c864c98965154df2c15f87c237a5ab83ea6f5c38898

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module ArticleFixtureGen
  module Data
    # Wraps attributes for a single marker-tag pair instance. Initially, the
    # only attribute supported is `:id_string`.
    class PmtpAttributes
      include Enumerable

      def initialize(config:)
        @items = build_all_items(config.pmtp_count, config.pmtp_text)
        self
      end

      def each(&_block)
        @items.each { |item| yield item }
      end

      private

      attr_reader :items

      def build_all_items(count, text)
        Array.new(count) { build_items(text) }.flatten
      end

      def build_items(text, rand_limit: 10_000)
        id_num = rand(rand_limit) + 1
        begin_item = Internals.build_item(text, id_num, 'begin')
        end_item = Internals.build_item(text, id_num, 'end')
        [end_item, begin_item]
      end

      # Stateless methods.
      module Internals
        def self.build_item(text, id_number, end_str)
          id_string = "#{text}-#{id_number}-#{end_str}"
          Struct.new(:id_string).new(id_string).freeze
        end
      end
      private_constant :Internals
    end # class ArticleFixtureGen::Data::PmtpAttributes
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
article_fixture_gen-0.1.2 lib/article_fixture_gen/data/pmtp_attributes.rb
article_fixture_gen-0.1.1 lib/article_fixture_gen/data/pmtp_attributes.rb