Class: ActionTree::CaptureHash Private

Inherits:
Hash
  • Object
show all
Defined in:
lib/action_tree/capture_hash.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Accumulates captured variables during a query match. Subclasses Hash, providing a non-destructive #add method, and merging.

Instance Method Summary collapse

Instance Method Details

#add(key, value) ⇒ CaptureHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add a specified value to a specified key.

Depending on the value already at the specified key:

  • Array: append the new value
  • nil: replace with new value
  • else: wrap existing value followed by new value in an Array

Returns:



36
37
38
39
40
41
42
43
# File 'lib/action_tree/capture_hash.rb', line 36

def add(key, value)
  case self[key]
    when nil    then self[key] = value
    when Array  then self[key] << value
    else             self[key] = [self[key], value]
  end
  self
end

#merge(*args) ⇒ CaptureHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nondestructive version of #merge!

Returns:



23
24
25
# File 'lib/action_tree/capture_hash.rb', line 23

def merge(*args)
  dup.merge!(*args)
end

#merge!(hsh) ⇒ CaptureHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

#add each key and value in the given Hash.

Returns:



15
16
17
18
# File 'lib/action_tree/capture_hash.rb', line 15

def merge!(hsh)
  hsh.each {|k,v| add(k,v) }
  self
end