Debian Codegraph @

plugin/spell/spell.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 require_relative 'error'
3 require_relative 'struct'
4
5 Plugin.create(:spell) do
6 defevent :search_spell, prototype: [Pluggaloid::COLLECT, Symbol, [Diva::Model], Hash]
7
8 defdsl :defspell do |spell_name, *constraint, condition: nil, &block|
9 beh = Plugin::Spell::Spell.new(spell_name.to_sym,
10 Set.new(constraint).freeze,
11 condition,
12 block)
13 if !respond_to?(spell_name)
14 defdsl spell_name do |*models|
15 spell(spell_name, *models)
16 end
17 defdsl :"#{spell_name}?" do |*models|
18 spell?(spell_name, *models)
19 end
20 end
21 filter_search_spell do |yielder, name, models, optional|
22 yielder << beh if beh.name == name.to_sym && beh.match(models, optional)
23 [yielder, name, models, optional]
24 end
25 end
26
27 defdsl :spell do |name, *models|
28 optional = {}.freeze
29 models = models.compact
30 *models, optional = models unless models.last.is_a?(Diva::Model)
31 Delayer::Deferred.new.next {
32 Plugin.collect(:search_spell, name.to_sym, models, optional).first
33 }.next do |spell|
34 raise Plugin::Spell::SpellNotFoundError, "Spell `#{name}' (#{models.map(&:class).join(', ')}) does not exists." unless spell
35 spell.call(models, optional)
36 end
37 end
38
39 defdsl :spell? do |name, *models|
40 optional = {}.freeze
41 models = models.compact
42 *models, optional = models unless models.last.is_a?(Diva::Model)
43 !Plugin.collect(:search_spell, name.to_sym, models, optional).first(1).empty?
44 end
45 end