Sha256: f9065e43deb5267bed10c8f29db8684be90649590b9648133d18eeacd52d1103

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module BitmaskEnum
  # Handles nil attribute values
  # @api private
  class NilHandler
    def initialize(handling_option)
      @handling_option = handling_option
    end

    # Handles nil when evaling the attribute
    # @param attribute [String] Name of the attribute
    # @return [String] Code string to handle a nil attribute according to the handling option
    def in_attribute_eval(attribute)
      select_handling(
        attribute,
        include: ->(attrib) { "(self['#{attrib}'] || 0)" }
      )
    end

    # Handles nil for an array of values for the attribute
    # @param array [Array] Array of integers representing values of the attribute
    # @return [Array] Array of integers representing values of the attribute, now corrected for nil values
    def in_array(array)
      select_handling(
        array,
        include: ->(arr) { arr << nil }
      )
    end

    private

    def select_handling(value, handling_actions)
      action = handling_actions[@handling_option] || handling_actions[:include]

      action.call(value)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bitmask_enum-1.1.1 lib/bitmask_enum/nil_handler.rb
bitmask_enum-1.1.0 lib/bitmask_enum/nil_handler.rb
bitmask_enum-1.0.0 lib/bitmask_enum/nil_handler.rb