Class: TypeBalancer::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/type_balancer/calculator.rb

Overview

Main calculator that handles type-based balancing of items

Constant Summary collapse

DEFAULT_TYPE_ORDER =
%w[video image strip article].freeze

Instance Method Summary collapse

Constructor Details

#initialize(items, type_field: :type, types: nil, type_order: nil, strategy: nil, **strategy_options) ⇒ Calculator

rubocop:disable Metrics/ParameterLists

Raises:

  • (ArgumentError)


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/type_balancer/calculator.rb', line 118

def initialize(items, type_field: :type, types: nil, type_order: nil, strategy: nil, **strategy_options)
  raise ArgumentError, 'Items cannot be nil' if items.nil?
  raise ArgumentError, 'Type field cannot be nil' if type_field.nil?

  @items = items
  @type_field = type_field
  @types = types
  @type_order = type_order
  @strategy_name = strategy
  @strategy_options = strategy_options
end

Instance Method Details

#callObject

rubocop:enable Metrics/ParameterLists



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/type_balancer/calculator.rb', line 131

def call
  return [] if @items.empty?

  # Create strategy instance with all options
  strategy = StrategyFactory.create(
    @strategy_name,
    items: @items,
    type_field: @type_field,
    types: @types || extract_types,
    type_order: @type_order,
    **@strategy_options
  )

  # Balance items using strategy
  strategy.balance
end