Module: Matchete::ClassMethods

Defined in:
lib/matchete.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_matcher(method_name) ⇒ Object



51
52
53
54
55
# File 'lib/matchete.rb', line 51

def convert_to_matcher(method_name)
  define_method(method_name) do |*args, **kwargs|
    call_overloaded(method_name, args: args, kwargs: kwargs)
  end
end

#default(method_name) ⇒ Object



30
31
32
33
# File 'lib/matchete.rb', line 30

def default(method_name)
  @default_methods[method_name] = instance_method(method_name)
  convert_to_matcher method_name
end

#either(*guards) ⇒ Object



35
36
37
# File 'lib/matchete.rb', line 35

def either(*guards)
  -> arg { guards.any? { |g| match_guard(g, arg) } }
end

#full_match(*guards) ⇒ Object



39
40
41
# File 'lib/matchete.rb', line 39

def full_match(*guards)
  -> arg { guards.all? { |g| match_guard(g, arg) } }
end

#on(*args, **kwargs) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/matchete.rb', line 15

def on(*args, **kwargs)
  if kwargs.count.zero?
    *guard_args, method_name = args
    guard_kwargs = {}
  else
    method_name = kwargs[:method]
    kwargs.delete :method
    guard_args = args
    guard_kwargs = kwargs
  end
  @methods[method_name] ||= []
  @methods[method_name] << [guard_args, guard_kwargs, instance_method(method_name)]
  convert_to_matcher method_name
end

#supporting(*method_names) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/matchete.rb', line 43

def supporting(*method_names)
  -> object do
    method_names.all? do |method_name|
      object.respond_to? method_name
    end
  end
end