Sha256: e2d6dfa4db04eea67d51e37b683247bbecc85a1dec0331e9f73e39f1c4f9f552

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

class ActiveSet
  class EnumerableSetInstruction < SimpleDelegator
    def initialize(attribute_instruction, set)
      @attribute_instruction = attribute_instruction
      @set = set
      super(@attribute_instruction)
    end

    def attribute_value_for(item)
      item_value = @attribute_instruction
                    .value_for(item: item)
      item_value = item_value.downcase if case_insensitive_operation_for?(item_value)
      item_value
    end

    def attribute_value
      _attribute_value = @attribute_instruction.value
      _attribute_value = _attribute_value.downcase if case_insensitive_operation_for?(_attribute_value)
      _attribute_value
    end

    def case_insensitive_operation_for?(value)
      return false unless @attribute_instruction.case_insensitive?

      value.is_a?(String) || value.is_a?(Symbol)
    end

    def attribute_instance
      set_item = @set.find(&:present?)
      return set_item if @attribute_instruction.associations_array.empty?
      return @attribute_model if defined? @attribute_model

      @attribute_model = @attribute_instruction
                         .associations_array
                         .reduce(set_item) do |obj, assoc|
        obj.public_send(assoc)
      end
    end

    def attribute_class
      attribute_instance&.class
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
actionset-0.8.1 lib/active_set/enumerable_set_instruction.rb