module Jimmy::Json::Collection

Common methods for {Hash} and {Array}

Public Instance Methods

[](key) click to toggle source

Get the member of the collection assigned to the given key.

# File lib/jimmy/json/collection.rb, line 33
def [](key)
  @members[cast_key(key)]
end
as_json(id: '', index: {}) click to toggle source

Transform the collection into plain JSON-compatible objects. @return [Hash, Array]

# File lib/jimmy/json/collection.rb, line 47
def as_json(id: '', index: {})
  return index[object_id].as_json(id: id, index: {}) if index[object_id]

  id = Json::URI.new(id)
  index[object_id] = Jimmy.ref(id)

  pairs = map do |key, value|
    if value.respond_to? :as_json
      value = value.as_json(id: id / key, index: index)
    end
    [key, value]
  end

  export_pairs pairs
end
clear() click to toggle source

Removes all members. @return [self]

# File lib/jimmy/json/collection.rb, line 65
def clear
  @members.clear
  self
end
deep_dup(index = {}) click to toggle source

Duplicate the collection and all of its members, recursively. @return [Jimmy::Json::Collection]

# File lib/jimmy/json/collection.rb, line 78
def deep_dup(index = {})
  return index[object_id] if index.key? object_id

  deep_dup_enumerable(self, index) { |new, k, v| new[k] = v }
end
dig(key, *rest) click to toggle source

@see Hash#dig

# File lib/jimmy/json/collection.rb, line 38
def dig(key, *rest)
  obj = self[cast_key(key)]
  return obj if obj.nil? || rest.empty?

  obj.dig(*rest)
end
dup() click to toggle source

Duplicate the collection. @return [Jimmy::Json::Collection]

# File lib/jimmy/json/collection.rb, line 72
def dup
  self.class.new @members
end
empty?() click to toggle source

Returns true if the collection has no members. @return [true, false]

# File lib/jimmy/json/collection.rb, line 21
def empty?
  @members.empty?
end
freeze() click to toggle source

Freeze the collection. @return [self]

Calls superclass method
# File lib/jimmy/json/collection.rb, line 27
def freeze
  @members.freeze
  super
end
inspect() click to toggle source

@see Object#inspect

# File lib/jimmy/json/collection.rb, line 15
def inspect
  to_json
end
to_json(**opts) click to toggle source

Serialize the collection as JSON.

# File lib/jimmy/json/collection.rb, line 10
def to_json(**opts)
  JSON.generate as_json, **opts
end

Protected Instance Methods

cast_value(value) click to toggle source
# File lib/jimmy/json/collection.rb, line 86
def cast_value(value)
  case value
  when nil, true, false, Numeric, String, Collection then value
  when ::Hash then Hash.new(value)
  when ::Array, Set then Array.new(value)
  else
    unless value.respond_to? :as_json
      raise Error::WrongType, "Incompatible JSON type #{value.class}"
    end

    value.as_json
  end
end
deep_dup_enumerable(value, index) { |new, *args, deep_dup_value(v, index)| ... } click to toggle source
# File lib/jimmy/json/collection.rb, line 106
def deep_dup_enumerable(value, index)
  index[value.object_id] = new = value.class.new
  value.each { |*args, v| yield new, *args, deep_dup_value(v, index) }
  new
end
deep_dup_value(value = self, index = {}) click to toggle source
# File lib/jimmy/json/collection.rb, line 100
def deep_dup_value(value = self, index = {})
  return value.deep_dup(index) if value.is_a? Collection

  value.dup
end