Class: OrderCompatibilityValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/validators/order_compatibility_validator.rb

Overview

Orders are compatible if: - all of the read lengths are identical - all of the request types are not for mutliplexing or - all of the request types post the multiplexing request are the same

Defined Under Namespace

Classes: OrderRequestTypes

Instance Method Summary collapse

Instance Method Details

#read_lengths_identical?(orders) ⇒ Boolean

rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity

Returns:

  • (Boolean)


27
28
29
# File 'app/validators/order_compatibility_validator.rb', line 27

def read_lengths_identical?(orders)
  orders.collect { |order| order.request_options['read_length'] }.uniq.length == 1
end

#validate(record) ⇒ Object

rubocop:todo Metrics/PerceivedComplexity, Metrics/MethodLength, Metrics/AbcSize



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/validators/order_compatibility_validator.rb', line 9

def validate(record) # rubocop:todo Metrics/CyclomaticComplexity
  orders = record.orders
  return if orders.size < 2

  record.errors.add(:orders, 'are incompatible') unless read_lengths_identical?(orders)
  order_request_types = orders.collect { |order| OrderRequestTypes.new(order.request_types) }
  return if order_request_types.all?(&:not_for_multiplexing?)

  record.errors.add(:orders, 'are incompatible') if order_request_types.any?(&:not_for_multiplexing?)
  unless order_request_types.all? { |request_types| # stree-ignore # rubocop:disable Style/BlockDelimiters
           request_types.post_for_multiplexing == order_request_types.first.post_for_multiplexing
         }
    record.errors.add(:orders, 'are incompatible')
  end
end