class Resolver

Resolves a client-side tag into a server-side instance.

Definitions

def self.allow(*arguments)

Creates an instance of the resolver, allowing the specified classes to be resolved.

Implementation

def self.allow(*arguments)
	self.new.allow(*arguments).freeze
end

def allow(*arguments)

Allow the specified classes to be resolved.

Implementation

def allow(*arguments)
	arguments.each do |klass|
		@allowed[klass.name] = klass
	end
	
	return self
end

def initialize

Initialize an empty resolver.

Implementation

def initialize
	@allowed = {}
end

attr :allowed

Signature

attribute Hash(String, Class)

A map of allowed class names.

def freeze

Freeze the resolver and its map of allowed classes.

Implementation

def freeze
	return self unless frozen?
	
	@allowed.freeze
	
	super
end

def root(view_class, id = view_class.unique_id, data: {}, **options)

Construct an allowed root element from its class.

Signature

parameter view_class Class

The view class to construct.

parameter id String

The unique identifier for the view.

parameter data Hash

The data associated with the view.

parameter options Hash

Additional options passed to the view constructor.

returns Element

A new view instance.

raises ArgumentError

If the view class is not allowed.

Implementation

def root(view_class, id = view_class.unique_id, data: {}, **options)
	unless @allowed[view_class.name].equal?(view_class)
		raise ArgumentError, "View class is not allowed: #{view_class.to_s.dump}!"
	end
	
	return make(view_class, id, data, **options)
end

def call(id, data)

Resolve a tag.

Signature

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.

Implementation

def call(id, data)
	if view_class = @allowed[data[:class]]
		return make(view_class, id, data)
	end
end