Sha256: 12f7fdea7c1acdaf26f339e651da4bf87ebfff7814bf76c6aeb87d36e2be3e3a

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 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_remove(items)
      keys = extract_keys(items)
      { "_type" => type_key, "_values" => compute_remove(items), "_deletes" => keys }
    end

    def compute_add(items)
      Array(items).inject(members) do |res, h|
        res.merge(h)
      end
    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.3.0 lib/rico/map.rb