Class: Quincunx::Method::Application

Inherits:
Struct
  • Object
show all
Defined in:
lib/quincunx/method.rb

Instance Method Summary collapse

Instance Method Details

#args_match?Boolean

Returns:

  • (Boolean)


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

def args_match?
  pairs.all? { |(a,b)| a.first === b }
end

#callObject



19
20
21
# File 'lib/quincunx/method.rb', line 19

def call
  env.instance_exec(&body)
end

#contains_right_keys?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/quincunx/method.rb', line 39

def contains_right_keys?
  pairs.all? do |(a,b)|
    (a[1] || {}).fetch(:keys, []).all? { |k| b.respond_to? k }
  end
end

#correct_arg_count?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/quincunx/method.rb', line 31

def correct_arg_count?
  matchers.count == args.count
end

#envObject



27
28
29
# File 'lib/quincunx/method.rb', line 27

def env
  @env ||= Environment.factory(obj, args, matchers)
end

#guardObject



53
54
55
# File 'lib/quincunx/method.rb', line 53

def guard
  params.last.is_a?(Hash) ? params.last.fetch(:when) : ->{ true }
end

#is_guard?(param) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/quincunx/method.rb', line 57

def is_guard?(param)
  param.is_a?(Hash) && param.keys == [:when]
end

#matchersObject



61
62
63
64
65
# File 'lib/quincunx/method.rb', line 61

def matchers
  @matchers ||= (is_guard?(params.last) ? params[0..-2] : params).map do |p|
    p.is_a?(Array) ? p : [p]
  end
end

#matches?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/quincunx/method.rb', line 23

def matches?
  correct_arg_count? && args_match? && contains_right_keys? && passes_guard?
end

#pairsObject



49
50
51
# File 'lib/quincunx/method.rb', line 49

def pairs
  matchers.zip(args)
end

#passes_guard?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/quincunx/method.rb', line 45

def passes_guard?
  env.instance_exec(&guard)
end