Module: DataProcessor::Manipulate

Included in:
DataProcessor
Defined in:
lib/data_processor/manipulate.rb

Instance Method Summary collapse

Instance Method Details

#manipulate(path, override = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/data_processor/manipulate.rb', line 4

def manipulate(path, override=false)
  obj = @data
  parent_obj = nil

  if block_given?
    counter = 0
    path_split = path.split("/")

    # down the rabbit hole
    path_split.each do |p|
      if obj && obj[p]
        parent_obj = obj
        obj = obj[p]
        counter = counter + 1
      else
        parent_obj = nil
        obj = nil
      end
    end

    # execute block with object as param
    # ie. if the object is found
    # + override object with return value of block if needed
    if obj && (counter == path_split.length)
      return_value = yield(obj, parent_obj)
      parent_obj[path_split.last] = return_value if override
    end
  end

  obj
end