Sha256: e909514f98c4635908625f77c92b3cd33ea901eb7896ade616d886ac3fda124e

Contents?: true

Size: 1.07 KB

Versions: 47

Compression:

Stored size: 1.07 KB

Contents

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 && record.items_key || LHS::Record.items_key
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
lhs-15.3.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.3.1.pre.fixlhc.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.3.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.5 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.4 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.3 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.3.pre.favorites.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.2.pre.favorites.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.2.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.1.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.1.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.0.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.0.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.0.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-14.6.5 lib/lhs/concerns/collection/handle_nested.rb
lhs-14.6.4 lib/lhs/concerns/collection/handle_nested.rb
lhs-14.6.3 lib/lhs/concerns/collection/handle_nested.rb
lhs-14.6.2 lib/lhs/concerns/collection/handle_nested.rb