Sha256: a6455c1e3e553742032a6de54f4ea79d3ab6a3498581d156c3b2190835d21f2e

Contents?: true

Size: 1.06 KB

Versions: 19

Compression:

Stored size: 1.06 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&.items_key || LHS::Record.items_key
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
lhs-16.1.5 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.1.4 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.1.3 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.1.2 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.1.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.1.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.0.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-16.0.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.7.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.6.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.6.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.5.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.5.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.4.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.4.0 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.4.0.pre.hasone.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.3.3 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.3.3.pre.fixoptions.1 lib/lhs/concerns/collection/handle_nested.rb
lhs-15.3.2 lib/lhs/concerns/collection/handle_nested.rb