Module: Phlex::Kit

Defined in:
lib/phlex/kit.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/phlex/kit.rb', line 15

def method_missing(name, *args, **kwargs, &block)
	if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
		public_send(name, *args, **kwargs, &block)
	else
		super
	end
end

Class Method Details

.extended(mod) ⇒ Object



4
5
6
7
# File 'lib/phlex/kit.rb', line 4

def self.extended(mod)
	warn "⚠️ [WARNING] Phlex::Kit is experimental and may be removed from future versions of Phlex."
	super
end

Instance Method Details

#const_added(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/phlex/kit.rb', line 31

def const_added(name)
	return if autoload?(name)

	constant = const_get(name)

	if Class === constant && constant < Phlex::SGML
		if instance_methods.include?(name)
			raise NameError, "The instance method `#{name}' is already defined on `#{inspect}`."
		elsif methods.include?(name)
			raise NameError, "The method `#{name}' is already defined on `#{inspect}`."
		end

		constant.include(self)

		define_method(name) do |*args, **kwargs, &block|
			render(constant.new(*args, **kwargs), &block)
		end

		define_singleton_method(name) do |*args, **kwargs, &block|
			if (component = Fiber[:__phlex_component__])
				component.instance_exec do
					render(constant.new(*args, **kwargs), &block)
				end
			else
				raise "You can't call `#{name}' outside of a Phlex rendering context."
			end
		end
	end

	super
end

#included(mod) ⇒ Object

When a kit is included in a module, we need to load all of its components.



10
11
12
13
# File 'lib/phlex/kit.rb', line 10

def included(mod)
	constants.each { |c| const_get(c) if autoload?(c) }
	super
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/phlex/kit.rb', line 23

def respond_to_missing?(name, include_private = false)
	if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)
		true
	else
		super
	end
end