Class: Quincunx::Method::Application
- Inherits:
-
Struct
- Object
- Struct
- Quincunx::Method::Application
- Defined in:
- lib/quincunx/method.rb
Instance Method Summary collapse
- #args_match? ⇒ Boolean
- #call ⇒ Object
- #contains_right_keys? ⇒ Boolean
- #correct_arg_count? ⇒ Boolean
- #env ⇒ Object
- #guard ⇒ Object
- #is_guard?(param) ⇒ Boolean
- #matchers ⇒ Object
- #matches? ⇒ Boolean
- #pairs ⇒ Object
- #passes_guard? ⇒ Boolean
Instance Method Details
#args_match? ⇒ Boolean
35 36 37 |
# File 'lib/quincunx/method.rb', line 35 def args_match? pairs.all? { |(a,b)| a.first === b } end |
#call ⇒ Object
19 20 21 |
# File 'lib/quincunx/method.rb', line 19 def call env.instance_exec(&body) end |
#contains_right_keys? ⇒ 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
31 32 33 |
# File 'lib/quincunx/method.rb', line 31 def correct_arg_count? matchers.count == args.count end |
#env ⇒ Object
27 28 29 |
# File 'lib/quincunx/method.rb', line 27 def env @env ||= Environment.factory(obj, args, matchers) end |
#guard ⇒ Object
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
57 58 59 |
# File 'lib/quincunx/method.rb', line 57 def is_guard?(param) param.is_a?(Hash) && param.keys == [:when] end |
#matchers ⇒ Object
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
23 24 25 |
# File 'lib/quincunx/method.rb', line 23 def matches? correct_arg_count? && args_match? && contains_right_keys? && passes_guard? end |
#pairs ⇒ Object
49 50 51 |
# File 'lib/quincunx/method.rb', line 49 def pairs matchers.zip(args) end |
#passes_guard? ⇒ Boolean
45 46 47 |
# File 'lib/quincunx/method.rb', line 45 def passes_guard? env.instance_exec(&guard) end |