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 a new instance of Vedeu::Models::Page.

Parameters:



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

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

Instance Attribute Details

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

Returns:



15
16
17
# File 'lib/vedeu/models/page.rb', line 15

def rows
  @rows
end

Class Method Details

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

Parameters:

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

Returns:

Raises:

  • (Vedeu::Error::InvalidSyntax)

    When the value given for an argument or parameter cannot be used because it is not valid for the use case, unsupported or the method expects a different type.



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

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) || value.is_a?(Vedeu::Buffers::View)
    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, Vedeu::Models::Page ' \
         "or Vedeu::Models::Row. Is a '#{value.class.name}'."

  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)


60
61
62
63
64
65
# File 'lib/vedeu/models/page.rb', line 60

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>)


53
54
55
# File 'lib/vedeu/models/page.rb', line 53

def content
  rows.flat_map(&:content)
end

#each(&block) ⇒ Enumerator

Provides iteration over the collection.

Parameters:

  • block (Proc)

Returns:

  • (Enumerator)


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

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

#empty?Boolean

Returns:



76
77
78
# File 'lib/vedeu/models/page.rb', line 76

def empty?
  rows.empty?
end

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

An object is equal when its values are the same.

Parameters:

Returns:



84
85
86
# File 'lib/vedeu/models/page.rb', line 84

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:



91
92
93
94
95
# File 'lib/vedeu/models/page.rb', line 91

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

  rows[index]
end

#sizeFixnum

Returns:

  • (Fixnum)


98
99
100
# File 'lib/vedeu/models/page.rb', line 98

def size
  rows.size
end