Sha256: 720543988ec99a35673752f82bb9e542232b4da132d66e26ec268a4cd42e9a24

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Models

    # A Page represents an array of Vedeu::Models::Row objects.
    #
    class Page

      include Enumerable

      # @!attribute [r] rows
      # @return [Array<NilClass|Vedeu::Models::Row>]
      attr_reader :rows

      class << self

        extend Forwardable

        def_delegators Vedeu::Coercers::Page,
                       :coerce

      end # Eigenclass

      # Returns a new instance of Vedeu::Models::Page.
      #
      # @param rows [Array<NilClass|Vedeu::Models::Row>]
      # @return [Vedeu::Models::Page]
      def initialize(rows = [])
        @rows = rows || []
      end

      # @return [Array<void>]
      def content
        rows.flat_map(&:content)
      end

      # @param row_index [Fixnum]
      # @param cell_index [Fixnum]
      # @return [NilClass|void]
      def cell(row_index = nil, cell_index = nil)
        return nil if row_index.nil? || cell_index.nil?
        return nil unless row(row_index)

        row(row_index)[cell_index]
      end

      # Provides iteration over the collection.
      #
      # @macro param_block
      # @return [Enumerator]
      def each(&block)
        rows.each(&block)
      end

      # @return [Boolean]
      def empty?
        rows.empty?
      end

      # An object is equal when its values are the same.
      #
      # @param other [Vedeu::Models::Page]
      # @return [Boolean]
      def eql?(other)
        self.class.equal?(other.class) && rows == other.rows
      end
      alias == eql?

      # @param index [Fixnum]
      # @return [NilClass|Vedeu::Models::Row]
      def row(index = nil)
        return nil if index.nil?

        rows[index]
      end

      # @return [Fixnum]
      def size
        rows.size
      end

    end # Page

  end # Models

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.8.23 lib/vedeu/models/page.rb
vedeu-0.8.22 lib/vedeu/models/page.rb
vedeu-0.8.21 lib/vedeu/models/page.rb