module ActsAsHoldable::Holdable::Core::ClassMethods

Public Instance Methods

initialize_acts_as_holdable_core() click to toggle source
# File lib/acts_as_holdable/holdable/core.rb, line 14
def initialize_acts_as_holdable_core
  set_options
end
validate_holding_options!(options) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

# File lib/acts_as_holdable/holdable/core.rb, line 19
def validate_holding_options!(options)
  unpermitted_params = []
  required_params = {}

  case holding_opts[:on_hand_type]
  when :closed
    required_params[:amount] = [Integer]
  when :open
    required_params[:amount] = [Integer]
  when :none
    unpermitted_params << :amount
  end

  # Actual validation
  unpermitted_params = unpermitted_params.select { |p| options.key?(p) }
                                         .map { |p| "'#{p}'" }

  wrong_types = required_params
                .select { |k, v| options.key?(k) && v.select { |type| options[k].is_a?(type) }.length.zero? }
                .map { |k, v| "'#{k}' must be a '#{v.join(' or ')}' but '#{options[k].class}' found" }

  required_params = required_params.reject { |k, _v| options.key?(k) }
                                   .map { |k, _v| "'#{k}'" }

  # Raise OptionsInvalid if some invalid parameters were found
  if (unpermitted_params.length + required_params.length + wrong_types.length).positive?
    message = ''
    message << " unpermitted parameters: #{unpermitted_params.join(',')}." unless unpermitted_params.empty?
    message << " missing parameters: #{required_params.join(',')}." unless required_params.empty?
    message << " parameters type mismatch: #{wrong_types.join(',')}" unless wrong_types.empty?
    raise ActsAsHoldable::OptionsInvalid.new(self, message)
  end
  true
end

Private Instance Methods

set_options() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/acts_as_holdable/holdable/core.rb, line 58
def set_options
  holding_opts[:preset] = :ticket

  # Validates options
  permitted_options = {
    on_hand_type: %i[open closed none],
    on_hold_track: [true, false],
    preset: [:ticket]
  }

  holding_opts.each_pair do |key, val|
    # rubocop:disable Style/GuardClause
    if !permitted_options.key? key
      raise ActsAsHoldable::InitializationError.new(self, "#{key} is not a valid option")
    elsif !permitted_options[key].include? val
      raise ActsAsHoldable::InitializationError.new(self, "#{val} is not a valid value for #{key}. Allowed values: #{permitted_options[key]}")
    end
    # rubocop:enable Style/GuardClause
  end

  defaults = case holding_opts[:preset]
             when :ticket
               {
                 on_hand_type: :open,
                 on_hold_track: true
               }
             else
               {
                 on_hand_type: :none,
                 on_hold_track: false
               }
             end

  # Merge options with defaults
  holding_opts.reverse_merge!(defaults)
end