Class: Contentful::Includes

Inherits:
Object
  • Object
show all
Includes:
ArrayLike
Defined in:
lib/contentful/includes.rb

Overview

The includes hashes returned when include_level is specified

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArrayLike

#[], #array?, #each_item, #empty?, #last, #size, #to_ary

Constructor Details

#initialize(items = [], lookup = nil) ⇒ Includes

Returns a new instance of Includes.



10
11
12
13
# File 'lib/contentful/includes.rb', line 10

def initialize(items = [], lookup = nil)
  self.items = items
  self.lookup = lookup || build_lookup
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/contentful/includes.rb', line 8

def items
  @items
end

#lookupObject

Returns the value of attribute lookup.



8
9
10
# File 'lib/contentful/includes.rb', line 8

def lookup
  @lookup
end

Class Method Details

.from_response(json, raw = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#+(other) ⇒ Object



42
43
44
45
46
47
48
49
50
# 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) ⇒ Object

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



38
39
40
# File 'lib/contentful/includes.rb', line 38

def ==(other)
  object_id == other.object_id || lookup == other.lookup
end

#dupObject



52
53
54
# File 'lib/contentful/includes.rb', line 52

def dup
  self.class.new(items.dup, lookup.dup)
end


29
30
31
32
# File 'lib/contentful/includes.rb', line 29

def find_link(link)
  key = "#{link['sys']['linkType']}:#{link['sys']['id']}"
  lookup[key]
end

#marshal_dumpObject



56
57
58
# File 'lib/contentful/includes.rb', line 56

def marshal_dump
  items
end

#marshal_load(array) ⇒ Object



60
61
62
63
# File 'lib/contentful/includes.rb', line 60

def marshal_load(array)
  self.items = array
  self.lookup = build_lookup
end