Sha256: 8c907585aaba65767dfa194fa630e35fca7ba2688b15b9134b0c38dbeed03dc2

Contents?: true

Size: 1.88 KB

Versions: 23

Compression:

Stored size: 1.88 KB

Contents

# encoding: utf-8
module Prismic
  module Fragments

    class GroupDocument
      include Prismic::WithFragments
      attr_accessor :fragments

      def initialize(fragments)
        @fragments = fragments
      end

      # Get a document's field
      # @return [Fragments::Fragment]
      def [](field)
        @fragments[field]
      end
      alias :get :[]

    end

    # A fragment of type Group, which contains an array of FragmentList (which
    # itself is a Hash of fragments).
    #
    # For instance, imagining this group is defined with two possible fragments:
    # an image fragment "image", and a text fragment "caption"; then accessing
    # the first image will look like this: `group[0]['image']`.
    class Group < Fragment

      # The array of group documents
      attr_accessor :group_documents

      def initialize(group_documents)
        @group_documents = group_documents
      end

      # Get the group document corresponding to index
      # @return [Prismic::WithFragments]
      def [](index)
        @group_documents[index]
      end

      alias :get :[]
      # @yieldparam group_doc [WithFragment]
      def each(&blk)
        @group_documents.each(&blk)
      end
      include Enumerable  # adds map, select, etc

      def length
        @group_documents.length
      end
      alias :size :length

      # Generate an HTML representation of the group
      #
      # @param link_resolver [LinkResolver] The LinkResolver used to build
      #     application's specific URL
      #
      # @return [String] the HTML representation
      def as_html(link_resolver = nil)
        @group_documents.map { |doc| doc.as_html(link_resolver) }.join("\n")
      end

      # Generate an text representation of the group
      #
      # @return [String] the text representation
      def as_text
        @group_documents.map { |doc| doc.as_text }.join("\n")
      end

    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
prismic.io-1.8.2 lib/prismic/fragments/group.rb
prismic.io-1.8.1 lib/prismic/fragments/group.rb
prismic.io-1.8.0 lib/prismic/fragments/group.rb
prismic.io-1.7.0 lib/prismic/fragments/group.rb
prismic.io-1.6.1 lib/prismic/fragments/group.rb
prismic.io-1.6.0 lib/prismic/fragments/group.rb
prismic.io-1.5.0 lib/prismic/fragments/group.rb
prismic.io-1.4.5 lib/prismic/fragments/group.rb
prismic.io-1.4.4 lib/prismic/fragments/group.rb
prismic.io-1.4.3 lib/prismic/fragments/group.rb
prismic.io-1.4.2 lib/prismic/fragments/group.rb
prismic.io-1.4.1 lib/prismic/fragments/group.rb
prismic.io-1.4.0 lib/prismic/fragments/group.rb
prismic.io-1.3.5 lib/prismic/fragments/group.rb
prismic.io-1.3.4 lib/prismic/fragments/group.rb
prismic.io-1.3.3 lib/prismic/fragments/group.rb
prismic.io-1.3.2 lib/prismic/fragments/group.rb
prismic.io-1.3.1 lib/prismic/fragments/group.rb
prismic.io-1.3.0 lib/prismic/fragments/group.rb
prismic.io-1.2.1 lib/prismic/fragments/group.rb