Class: PatternProc::PatternProcCase
- Inherits:
-
Object
- Object
- PatternProc::PatternProcCase
- Defined in:
- lib/pattern-proc/pattern_proc_case.rb
Instance Attribute Summary collapse
-
#proc_applied_level ⇒ Object
readonly
Returns the value of attribute proc_applied_level.
-
#proc_arity ⇒ Object
readonly
Returns the value of attribute proc_arity.
Instance Method Summary collapse
- #arity ⇒ Object
-
#expected_arity ⇒ Object
private ish.
-
#initialize(args, previous_specificity = 0, proc_arity = 0, &block) ⇒ PatternProcCase
constructor
A new instance of PatternProcCase.
- #make_subcase(args) ⇒ Object
- #returns(value) ⇒ Object
- #specificity ⇒ Object
- #to_proc ⇒ Object
Constructor Details
#initialize(args, previous_specificity = 0, proc_arity = 0, &block) ⇒ PatternProcCase
Returns a new instance of PatternProcCase.
7 8 9 10 11 12 13 14 15 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 7 def initialize(args, previous_specificity = 0, proc_arity = 0, &block) @expected_args = args @proc = block @proc_arity = proc_arity @previous_specificity = previous_specificity || 0 if !block.nil? && block.arity > 0 @proc_arity = block.arity end end |
Instance Attribute Details
#proc_applied_level ⇒ Object (readonly)
Returns the value of attribute proc_applied_level.
4 5 6 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 4 def proc_applied_level @proc_applied_level end |
#proc_arity ⇒ Object (readonly)
Returns the value of attribute proc_arity.
5 6 7 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 5 def proc_arity @proc_arity end |
Instance Method Details
#arity ⇒ Object
27 28 29 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 27 def arity expected_arity + proc_arity end |
#expected_arity ⇒ Object
private ish
23 24 25 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 23 def expected_arity @expected_args.size || 0 end |
#make_subcase(args) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 35 def make_subcase(args) match_len = [args.size, @expected_args.size].min expected = @expected_args.take(match_len) actual = args.take(match_len) if expected == actual new_proc = to_proc.curry curry_count = args.size - @expected_args.size new_arity = proc_arity if curry_count > 0 send_args = args.drop(match_len).take(curry_count) new_proc = new_proc.call(*send_args) new_arity -= curry_count if new_arity == 0 return PatternProcCase.new([], specificity).returns(new_proc) elsif !(new_proc.is_a?(Proc)) raise "uh oh" end end PatternProcCase.new(@expected_args.drop(match_len), specificity, new_arity, &new_proc).returns(@return_value) else nil end end |
#returns(value) ⇒ Object
17 18 19 20 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 17 def returns(value) @return_value = value self end |
#specificity ⇒ Object
31 32 33 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 31 def specificity expected_arity + @previous_specificity end |
#to_proc ⇒ Object
59 60 61 |
# File 'lib/pattern-proc/pattern_proc_case.rb', line 59 def to_proc @proc || ->(*args) { @return_value } end |