Module: TypeBalancer

Defined in:
lib/type_balancer.rb,
lib/type_balancer/version.rb,
lib/type_balancer/balancer.rb,
lib/type_balancer/calculator.rb,
lib/type_balancer/strategies.rb,
lib/type_balancer/distributor.rb,
lib/type_balancer/configuration.rb,
lib/type_balancer/type_extractor.rb,
lib/type_balancer/batch_processing.rb,
lib/type_balancer/ratio_calculator.rb,
lib/type_balancer/strategy_factory.rb,
lib/type_balancer/sequential_filler.rb,
lib/type_balancer/alternating_filler.rb,
lib/type_balancer/position_calculator.rb,
lib/type_balancer/distribution_calculator.rb,
lib/type_balancer/type_extractor_registry.rb,
lib/type_balancer/strategies/base_strategy.rb,
lib/type_balancer/ordered_collection_manager.rb,
lib/type_balancer/strategies/sliding_window_strategy.rb

Defined Under Namespace

Modules: Distributor, RatioCalculator, Strategies Classes: AlternatingFiller, Balancer, BatchProcessing, Calculator, Configuration, ConfigurationError, DistributionCalculator, EmptyCollectionError, Error, InvalidTypeError, OrderedCollectionManager, PositionCalculator, SequentialFiller, StrategyFactory, TypeExtractor, TypeExtractorRegistry, ValidationError

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.balance(items, type_field: :type, type_order: nil, strategy: nil, window_size: nil, **strategy_options) ⇒ Object

rubocop:disable Metrics/ParameterLists



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/type_balancer.rb', line 48

def self.balance(items, type_field: :type, type_order: nil, strategy: nil, window_size: nil, **strategy_options)
  # Input validation
  raise EmptyCollectionError, 'Collection cannot be empty' if items.empty?

  # Use centralized extractor
  extractor = TypeExtractorRegistry.get(type_field)
  begin
    types = extractor.extract_types(items)
    raise Error, "Invalid type field: #{type_field}" if types.empty?
  rescue Error => e
    raise Error, "Cannot access type field '#{type_field}': #{e.message}"
  end

  # Merge window_size into strategy_options if provided
  strategy_options = strategy_options.merge(window_size: window_size) if window_size

  # Create calculator with strategy options
  calculator = Calculator.new(
    items,
    type_field: type_field,
    types: type_order || types,
    type_order: type_order,
    strategy: strategy,
    **strategy_options
  )

  # Balance items
  calculator.call
end

.calculate_positions(total_count:, ratio:, available_items: nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/type_balancer.rb', line 39

def self.calculate_positions(total_count:, ratio:, available_items: nil)
  PositionCalculator.calculate_positions(
    total_count: total_count,
    ratio: ratio,
    available_items: available_items
  )
end

.extract_types(items, type_field) ⇒ Object

Backward compatibility methods



80
81
82
83
84
85
# File 'lib/type_balancer.rb', line 80

def self.extract_types(items, type_field)
  TypeExtractorRegistry.get(type_field).extract_types(items)
rescue Error
  # For backward compatibility, return array with nil for inaccessible type fields
  [nil]
end