Sha256: 799ed426d5a211094def7ed27c169e0e321938e2cbefe7dfdf9c5d110a6c35b1

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Depth
  module Actions
    def set(route, value)
      route = RouteElement.convert_route(route)
      object = route[0 ... -1].reduce(Traverser.new(hsh)) { |t, route_el|
        t.next_or_create(route_el.key) { route_el.create }
      }.object
      object[route.last.key] = value
    end

    def find(route)
      route = RouteElement.convert_route(route)
      route.reduce(Traverser.new(hsh)) { |t, route_el|
        t.next(route_el.key)
      }.object
    end

    def alter(route, key: nil, value: nil)
      return set(route, value) if key == nil
      route = RouteElement.convert_route(route)
      value = find(route) unless value
      new_route = (route[0 ... -1] << RouteElement.convert(new_key))
      set(new_route, value) # ensure it exists
      old_key = route.last.key
      return unless old_key != new_key
      delete(route)
    end

    def delete(route)
      route = RouteElement.convert_route(route)
      traverser = route[0...-1].reduce(Traverser.new(hsh)) do |t, route_el|
        t.next(route_el.key)
      end
      if traverser.array?
        traverser.object.delete_at(route.last.key)
      elsif traverser.hash?
        traverser.object.delete(route.last.key)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
depth-0.0.1 lib/depth/actions.rb