Class: Vedeu::Models::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vedeu/models/page.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows = []) ⇒ Vedeu::Models::Page

Returns an instance of Vedeu::Models::Page.

Parameters:



44
45
46
# File 'lib/vedeu/models/page.rb', line 44

def initialize(rows = [])
  @rows = rows || []
end

Instance Attribute Details

#rowsArray<NilClass|Vedeu::Models::Row> (readonly)

Returns:



13
14
15
# File 'lib/vedeu/models/page.rb', line 13

def rows
  @rows
end

Class Method Details

.coerce(value) ⇒ Vedeu::Models::Page

Parameters:

  • value
    Vedeu::Models::Page|Vedeu::Models::Row|Array<void>|void

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vedeu/models/page.rb', line 18

def self.coerce(value)
  if value.is_a?(Vedeu::Models::Page)
    value

  elsif value.is_a?(Vedeu::Models::Row)
    Vedeu::Models::Page.new([value])

  elsif value.is_a?(Array) && value.empty?
    Vedeu::Models::Page.new([Vedeu::Models::Row.coerce(value)])

  elsif value.is_a?(Array)
    values = value.map { |v| Vedeu::Models::Row.coerce(v) }

    Vedeu::Models::Page.new(values)

  else
    fail Vedeu::Error::InvalidSyntax,
         'Cannot coerce as value is not an Array.'.freeze

  end
end

Instance Method Details

#cell(row_index = nil, cell_index = nil) ⇒ NilClass|void

Parameters:

  • row_index (Fixnum) (defaults to: nil)
  • cell_index (Fixnum) (defaults to: nil)

Returns:

  • (NilClass|void)


56
57
58
59
60
61
# File 'lib/vedeu/models/page.rb', line 56

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

#contentArray<void>

Returns:

  • (Array<void>)


49
50
51
# File 'lib/vedeu/models/page.rb', line 49

def content
  rows.flat_map(&:content)
end

#each(&block) ⇒ Enumerator

Provides iteration over the collection.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


67
68
69
# File 'lib/vedeu/models/page.rb', line 67

def each(&block)
  rows.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/vedeu/models/page.rb', line 72

def empty?
  rows.empty?
end

#eql?(other) ⇒ Boolean Also known as: ==

An object is equal when its values are the same.

Parameters:

Returns:

  • (Boolean)


80
81
82
# File 'lib/vedeu/models/page.rb', line 80

def eql?(other)
  self.class == other.class && rows == other.rows
end

#row(index = nil) ⇒ NilClass|Vedeu::Models::Row

Parameters:

  • index (Fixnum) (defaults to: nil)

Returns:



87
88
89
90
91
# File 'lib/vedeu/models/page.rb', line 87

def row(index = nil)
  return nil if index.nil?

  rows[index]
end