Class: PrismicRails::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/prismic_rails/content/result.rb

Overview

The PrismicRails::Result object is a wrapper object around the Prismic::Response. The purpose of this wrapper object is to have an easy access on the included documents or to query a specific fragment.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Result

Creates the PrismicRails::Result object with the initial documents.

Attributes

+response+ The response of the Prismic API query


15
16
17
18
19
20
21
22
23
24
# File 'lib/prismic_rails/content/result.rb', line 15

def initialize(response)
  if response && response.results && !response.results.empty?
    @documents = response.results.map do |document|
      PrismicRails::Document.new(document)
    end
  else #Handles the case if the response is nil or empty
    nil_document = PrismicRails::NilDocument.new
    @documents = [nil_document]
  end
end

Instance Attribute Details

#documentsObject

Returns the value of attribute documents.



9
10
11
# File 'lib/prismic_rails/content/result.rb', line 9

def documents
  @documents
end

Instance Method Details

#find_fragment(type) ⇒ Object

Find a specific fragment in the list of all document.

Examples

PrismicRails::Result.find_fragment('image')

This call walks through all the document and looks for a fragment with the type ‘image’.



33
34
35
36
37
# File 'lib/prismic_rails/content/result.rb', line 33

def find_fragment(type)
  @documents.each do |document|
    return document.find_fragment(type)
  end
end

#firstObject



39
40
41
# File 'lib/prismic_rails/content/result.rb', line 39

def first
  @documents.first
end

#lastObject



43
44
45
# File 'lib/prismic_rails/content/result.rb', line 43

def last
  @documents.last
end