class Contentful::Includes

The includes hashes returned when include_level is specified

Attributes

items[RW]
lookup[RW]

Public Class Methods

from_response(json, raw = true) click to toggle source
# File lib/contentful/includes.rb, line 15
def self.from_response(json, raw = true)
  includes = if raw
               json['items'].dup
             else
               json['items'].map(&:raw)
             end

  %w[Entry Asset].each do |type|
    includes.concat(json['includes'].fetch(type, [])) if json.fetch('includes', {}).key?(type)
  end

  new includes || []
end
new(items = [], lookup = nil) click to toggle source
# File lib/contentful/includes.rb, line 10
def initialize(items = [], lookup = nil)
  self.items = items
  self.lookup = lookup || build_lookup
end

Public Instance Methods

+(other) click to toggle source
# File lib/contentful/includes.rb, line 42
def +(other)
  # If we're being asked to add to itself, just return without duplicating
  return self if self == other

  dup.tap do |copy|
    copy.items += other.items
    copy.lookup.merge!(other.lookup)
  end
end
==(other) click to toggle source

If the lookups are the same then these two objects are effectively the same

# File lib/contentful/includes.rb, line 38
def ==(other)
  object_id == other.object_id || lookup == other.lookup
end
dup() click to toggle source
# File lib/contentful/includes.rb, line 52
def dup
  self.class.new(items.dup, lookup.dup)
end
marshal_dump() click to toggle source
# File lib/contentful/includes.rb, line 56
def marshal_dump
  items
end
marshal_load(array) click to toggle source
# File lib/contentful/includes.rb, line 60
def marshal_load(array)
  self.items = array
  self.lookup = build_lookup
end

Private Instance Methods

build_lookup() click to toggle source
# File lib/contentful/includes.rb, line 67
def build_lookup
  items.each_with_object({}) do |i, h|
    key = "#{i['sys']['type']}:#{i['sys']['id']}"
    h[key] = i
  end
end