Sha256: e8fc5a57ed3b8c85010dc24721ed3bf76c31d02aed8f6a6215bd1c471b356529

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2021-2024, by Samuel Williams.

require_relative 'element'

module Live
	# Resolves a client-side tag into a server-side instance.
	class Resolver
		# Creates an instance of the resolver, allowing the specified classes to be resolved.
		def self.allow(*arguments)
			self.new.allow(*arguments).freeze
		end
		
		def initialize
			@allowed = {}
		end
		
		# @attribute [Hash(String, Class)] A map of allowed class names.
		attr :allowed
		
		def freeze
			return self unless frozen?
			
			@allowed.freeze
			
			super
		end
		
		# Allow the specified classes to be resolved.
		def allow(*arguments)
			arguments.each do |klass|
				@allowed[klass.name] = klass
			end
			
			return self
		end
		
		# Resolve a tag.
		# @parameter id [String] The unique identifier for the tag.
		# @parameter data [Hash] The data associated with the tag. Should include the `:class` key.
		# @returns [Element] The element instance if it was allowed.
		def call(id, data)
			if klass = @allowed[data[:class]]
				return klass.new(id, **data)
			end
		end
	end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
live-0.12.0 lib/live/resolver.rb
live-0.9.0 lib/live/resolver.rb
live-0.8.1 lib/live/resolver.rb
live-0.8.0 lib/live/resolver.rb
live-0.7.0 lib/live/resolver.rb
live-0.6.0 lib/live/resolver.rb