lib/phlex/kit.rb
Lines
78%30 of 38 covered
Branches
43%14 of 32 covered
Methods
40%13 of 32 covered
- Not run
- 1115-1618253943-44
- Never called
Phlex::Kit::LazyLoader#respond_to_missing?Phlex::Kit#respond_to_missing?Example#PageExample#LayoutComponentExample#DynamicListComponents::Foo#BarComponents#TitleComponents#SubtitleKitTest::ShadowingComponents#ThreadKitTest::ShadowingComponents#CardKitTest::KeywordComponents#ENDKitTest::RactorComponents#Rules#<Class:KitTest::ShadowingComponents>#Thread#<Class:KitTest::KeywordComponents>#BEGIN#<Class:Components>#Article#<Class:Example>#DynamicList#<Class:Example>#LayoutComponent#<Class:Example>#Page#<Class:KitTest::RactorComponents>#Rules
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | 1 | module Phlex::Kit |
| 4 | 1 | module LazyLoader |
| 5 | 1 | def method_missing(name, ...) |
| 6 | 1 | mod = self.class |
| 7 | ||
| 8 | 1 | if name[0] == name[0].upcase && mod.constants.include?(name) && mod.const_get(name) && methods.include?(name) |
| 9 | 1 | public_send(name, ...) |
| 10 | else | |
| 11 | 0 | superelse never taken |
| 12 | end | |
| 13 | end | |
| 14 | ||
| 15 | 0 | def respond_to_missing?(name, include_private = false) |
| 16 | 0 | mod = self.class |
| 17 | ||
| 18 | 0 | (name[0] == name[0].upcase && mod.constants.include?(name) && mod.const_get(name) && methods.include?(name)) || super |
| 19 | end | |
| 20 | end | |
| 21 | ||
| 22 | 1 | def self.extended(mod) |
| 23 | 7 | case mod |
| 24 | when Class | |
| 25 | 0 | raise Phlex::ArgumentError.new(<<~MESSAGE)when never taken |
| 26 | `Phlex::Kit` was extended into #{mod.name}. | |
| 27 | ||
| 28 | You should only extend modules with `Phlex::Kit` as it is not compatible with classes. | |
| 29 | MESSAGE | |
| 30 | else | |
| 31 | 7 | mod.include(LazyLoader) |
| 32 | end | |
| 33 | end | |
| 34 | ||
| 35 | 1 | def method_missing(name, ...) |
| 36 | 1 | if name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name) |
| 37 | 1 | public_send(name, ...) |
| 38 | else | |
| 39 | 0 | superelse never taken |
| 40 | end | |
| 41 | end | |
| 42 | ||
| 43 | 0 | def respond_to_missing?(name, include_private = false) |
| 44 | 0 | (name[0] == name[0].upcase && constants.include?(name) && const_get(name) && methods.include?(name)) || super |
| 45 | end | |
| 46 | ||
| 47 | 1 | def const_added(name) |
| 48 | 19 | return if autoload?(name) |
| 49 | ||
| 50 | 17 | constant = const_get(name) |
| 51 | ||
| 52 | 17 | case constantelse never taken |
| 53 | when Class | |
| 54 | 16 | if constant < Phlex::SGMLelse never taken |
| 55 | 16 | constant.include(self) |
| 56 | ||
| 57 | # Defined from a string so they can be called from any Ractor. | |
| 58 | 16 | reference = (name in :BEGIN | :END) ? "::Module.nesting.first.const_get(:#{name})" : name |
| 59 | ||
| 60 | 16 | module_eval(<<~RUBY, __FILE__, __LINE__ + 1) |
| 61 | 5 | def #{name}(*, **, &) |
| 62 | 3 | render(#{reference}.new(*, **), &) |
| 63 | end | |
| 64 | ||
| 65 | 5 | def self.#{name}(*args, **kwargs, &block) |
| 66 | 4 | component = ::Thread.current[:__phlex_component__] |
| 67 | 4 | if component |
| 68 | 3 | component.instance_exec do |
| 69 | 3 | render(#{reference}.new(*args, **kwargs), &block) |
| 70 | end | |
| 71 | else | |
| 72 | 1 | raise "You can't call `#{name}' outside of a Phlex rendering context."else never takenelse never takenelse never takenelse never takenelse never taken |
| 73 | end | |
| 74 | end | |
| 75 | RUBY | |
| 76 | end | |
| 77 | when Module | |
| 78 | 1 | constant.extend(Phlex::Kit) |
| 79 | end | |
| 80 | ||
| 81 | 17 | super |
| 82 | end | |
| 83 | end |