Sha256: 5337f0ff4cf9780a900ea13409d0580dcd8d5a5e05b774074936db66fb03b78f

Contents?: true

Size: 1.09 KB

Versions: 76

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require 'active_support'

class LHS::Collection < LHS::Proxy

  # Handles pontentially (deep-)nested collections
  #   Examples:
  #     [ { name: 'Steve '} ]
  #     { items: [ { name: 'Steve' } ] }
  #     { response: { business: [ { name: 'Steve' } ] } }
  module HandleNested
    extend ActiveSupport::Concern

    delegate :access, :nest, :concat, to: :class

    module ClassMethods
      # Access potentially nested collection of items
      def access(input:, record: nil)
        input.dig(*items_key(record))
      end

      # Initializes nested collection
      def nest(input:, value: nil, record: nil)
        input[items_key(record)] = value
      end

      # Concats existing nested collection of items
      # with given items
      def concat(input:, items:, record: nil)
        input.dig(*items_key(record)).concat(items)
      end

      private

      # Takes configured items key to access collection of items
      # of falls back to the default key
      def items_key(record)
        record&.items_key || LHS::Record.items_key
      end
    end
  end
end

Version data entries

76 entries across 76 versions & 1 rubygems

Version Path
lhs-26.2.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-26.1.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-26.0.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-26.0.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.2.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.1.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.0.4 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.0.3 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.0.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.0.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-25.0.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.1.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.1.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.1.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.1.0.pre.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.1.0.pre.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-24.0.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-23.0.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-23.0.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-23.0.0 lib/lhs/concerns/collection/handle_nested.rb