3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
62
63
|
# File 'lib/rulix/registry.rb', line 3
def self.included other
other.class_eval do
@registry ||= {}
def self.register symbol, procable = nil, &block
return register_block symbol, &block if block_given?
if !procable.respond_to?(:to_proc)
unless (procable.respond_to?(:new) && procable.new.respond_to?(:to_proc))
raise ArgumentError, "You attempted to register :#{symbol}, but the argument you passed can't be coerced into a proc!"
end
end
@registry[symbol] = procable
end
private
def self.register_block symbol, &block
@registry[symbol] = block.to_proc
end
def self.get_operations operations
operations.map do |op|
get_operation op
end
end
def self.get_operation operation
case operation
when Symbol
return @registry[operation].to_proc if @registry[operation] && @registry[operation].respond_to?(:to_proc)
operation.to_proc
when Hash
key = operation.keys.first
arguments = operation[key]
registered_procable = @registry[key]
raise ArgumentError, "You've supplied a hash argument for a rule, but there's no rule registered for #{key}!" unless registered_procable
case registered_procable
when Proc
registered_procable.curry[arguments ]
else
registered_procable.new(arguments).to_proc
end
when Proc
operation
else
raise ArgumentError, "Can't coerce #{operation} into useable proc!" unless operation.respond_to? :to_proc
operation.to_proc
end
end
end
end
|