Sha256: e2414858bedefb8388287d106ed8e2ee210e9a56eb9f38e9da668f4d4d6584dc

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Pakyow
  module UI
    # Helpers for building channel names.
    #
    # @api private
    module ChannelBuilder
      PARTS = [:scope, :mutation, :component]

      def self.build(qualifiers: [], data: [], qualifications: {}, **args)
        channel = []
        channel_extras = []

        PARTS.each do |part|
          add_part(part, args[part], channel)
        end

        add_qualifiers(qualifiers, data, channel_extras)
        add_qualifications(qualifications, channel_extras)

        channel = channel.join(';')

        return channel if channel_extras.empty?
        channel << "::#{channel_extras.join(';')}"
      end

      private

      def self.add_part(part, value, channel)
        return if value.nil?
        channel << "#{part}:#{value}"
      end

      def self.add_qualifiers(qualifiers, data, channel_extras)
        qualifiers = Array.ensure(qualifiers)

        data = data.data if data.is_a?(Pakyow::UI::MutableData)
        return if qualifiers.empty? || data.empty?

        datum = Array.ensure(data).first

        qualifiers.each do |qualifier|
          channel_extras << "#{qualifier}:#{datum[qualifier]}"
        end
      end

      def self.add_qualifications(qualifications, channel_extras)
        qualifications.each do |name, value|
          next if value.nil?
          channel_extras << "#{name}:#{value}"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pakyow-ui-0.10.2 pakyow-ui/lib/pakyow-ui/channel_builder.rb
pakyow-ui-0.10.1 pakyow-ui/lib/pakyow-ui/channel_builder.rb
pakyow-ui-0.10.0 pakyow-ui/lib/pakyow-ui/channel_builder.rb