Sha256: 4c97987fd2799d99b052d60bb8a4f30db587a751a7621542f03e2e075aac0273

Contents?: true

Size: 835 Bytes

Versions: 1

Compression:

Stored size: 835 Bytes

Contents

class DataProcessor
  module Manipulate

    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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_processor-0.1.0 lib/data_processor/manipulate.rb