Class: Pablo::Arguments

Inherits:
Object
  • Object
show all
Defined in:
lib/pablo/arguments.rb

Overview

Safely encapsules the arguments and provides means to manipulate them

Instance Method Summary collapse

Constructor Details

#initialize(arr) ⇒ Arguments

Returns a new instance of Arguments.



38
39
40
# File 'lib/pablo/arguments.rb', line 38

def initialize arr
    @slices = [{:idx => 0, :arr => arr}]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



34
35
36
# File 'lib/pablo/arguments.rb', line 34

def method_missing sym, *args, &block
    @slices[-1][:arr].send(sym, *args, &block)
end

Instance Method Details

#consume(idx) ⇒ Object



42
43
44
# File 'lib/pablo/arguments.rb', line 42

def consume idx
    @slices[-1][:arr].delete_at idx
end

#consume_allObject



50
51
52
# File 'lib/pablo/arguments.rb', line 50

def consume_all
    @slices[-1][:arr].clear
end

#consumed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/pablo/arguments.rb', line 46

def consumed?
    @slices[-1][:arr].empty?
end

#slice(idx) ⇒ Object

Constrain arguments on the subset idx..-1.



56
57
58
59
60
61
# File 'lib/pablo/arguments.rb', line 56

def slice idx
    @slices << {
        :idx => idx,
        :arr => @slices[-1][:arr][idx..-1]
    }
end

#unsliceObject

Remove arguments constrain and reconcile arguments with slice.



66
67
68
69
70
71
# File 'lib/pablo/arguments.rb', line 66

def unslice
    raise "cannot unslice fully unsliced Arguments" unless @slices.length > 1
    idx, arr = @slices[-1][:idx], @slices[-1][:arr]
    @slices[-2][:arr][idx..-1] = arr
    @slices.delete_at(-1)
end