Sha256: cbe37d7e215654f150abe336bc452bd3bf239366f934c9295cfd612855b59657

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Rico
  class Map < Collection

    public

    # Obtains the items in the array
    #
    # Returns the data in the object as an array
    def members
      ((data || {})["_values"] || {})
    end

    # Resolve conflict between one or more RObject siblings
    #
    # robjects - array of RObjects to merge
    #
    # Returns a single RObject result or nil
    def self.resolve(robject)
      siblings = robject.siblings
      values = siblings.map {|r| (r.data["_values"] || {}) }
      deletions = siblings.map {|r| Array(r.data["_deletes"]) }.flatten
      result = values.inject({}) {|res, h| res.merge(h) }.reject {|k,v| deletions.include? k }

      obj = robject.dup
      obj.siblings = [obj.siblings.first]
      obj.data = { "_values" => result, "_deletes" => deletions }
      obj
    end

    protected

    def build_map_add(items)
      { "_type" => type_key, "_values" => compute_add(items) }
    end

    def build_map_remove(items)
      keys = extract_keys(items)
      { "_type" => type_key, "_values" => compute_remove(items), "_deletes" => keys }
    end

    def compute_add(items)
      members.merge(items)
    end

    def compute_remove(items)
      keys = extract_keys(items)
      members.delete_if {|k,v| keys.include? k.to_s }
    end

    def extract_keys(items)
      Array(items).map {|i| i.respond_to?(:keys) ? i.keys : i }.flatten.map(&:to_s)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rico-0.4.0 lib/rico/map.rb