Class: Deep::Hash::Struct::Dashboard::Table

Inherits:
Object
  • Object
show all
Includes:
PP::Dashboard::Table
Defined in:
lib/deep/hash/struct/dashboard/table.rb,
lib/deep/hash/struct/dashboard/table/row.rb

Defined Under Namespace

Classes: Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PP::Dashboard::Table

#inspect, #pretty_print

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



19
20
21
22
23
24
25
26
27
# File 'lib/deep/hash/struct/dashboard/table.rb', line 19

def initialize(options = {})
  self.opt           = default_options.merge(options)
  self.header        = Wrapper.new
  self.side          = Wrapper.new
  self.body          = []
  self.bodies        = []
  self.rows          = []
  header.side_header = opt[:side_header] if matrix?
end

Instance Attribute Details

#bodiesObject

Returns the value of attribute bodies.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def bodies
  @bodies
end

#bodyObject

Returns the value of attribute body.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def body
  @body
end

#headerObject

Returns the value of attribute header.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def header
  @header
end

#headersObject

Returns the value of attribute headers.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def headers
  @headers
end

#optObject

Returns the value of attribute opt.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def opt
  @opt
end

#rowsObject

Returns the value of attribute rows.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def rows
  @rows
end

#sideObject

Returns the value of attribute side.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def side
  @side
end

#sidesObject

Returns the value of attribute sides.



10
11
12
# File 'lib/deep/hash/struct/dashboard/table.rb', line 10

def sides
  @sides
end

Instance Method Details

#add_body {|b = Wrapper.new| ... } ⇒ Object

Yields:



41
42
43
44
# File 'lib/deep/hash/struct/dashboard/table.rb', line 41

def add_body
  yield b = Wrapper.new
  body << b
end

#add_header {|header| ... } ⇒ Object

Yields:



33
34
35
# File 'lib/deep/hash/struct/dashboard/table.rb', line 33

def add_header
  yield header
end

#add_side {|side| ... } ⇒ Object

Yields:



37
38
39
# File 'lib/deep/hash/struct/dashboard/table.rb', line 37

def add_side
  yield side
end

#eachObject



88
89
90
91
92
93
94
95
96
# File 'lib/deep/hash/struct/dashboard/table.rb', line 88

def each
  if block_given?
    rows.each do |r|
      yield r
    end
  else
    rows.each
  end
end

#mapObject Also known as: collect



98
99
100
101
102
103
104
105
106
# File 'lib/deep/hash/struct/dashboard/table.rb', line 98

def map
  if block_given?
    rows.map do |r|
      yield r
    end
  else
    rows.map
  end
end

#matrix?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/deep/hash/struct/dashboard/table.rb', line 29

def matrix?
  opt[:matrix]
end

#parse!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deep/hash/struct/dashboard/table.rb', line 46

def parse!
  raise_check!

  self.headers = header.map.each_with_index do |(k, v), i|
    Row.new(header: true, side: matrix? && i.zero?, name: v, value: k)
  end

  self.sides = side.map do |k, v|
    Row.new(side: true, name: v, value: k)
  end

  self.bodies = if matrix?
                  merge_body = body.shift
                  body.size.times do
                    bs.merge! body.shift
                  end

                  sides.map do |s|
                    rs  = []
                    rs << s
                    rs += headers.map do |h|
                      next if h.side_header?
                      v = merge_body.to_h.dig(h.value, s.value)
                      v = v.nil? ? opt[:default] : v
                      Row.new(value: v)
                    end.compact
                    rs
                  end
                else
                  body.map do |r|
                    headers.map do |h|
                      v = r.to_h.dig(h.value)
                      v = v.nil? ? opt[:default] : v
                      Row.new(value: v)
                    end.compact
                  end
                end

  self.rows << headers
  self.rows += bodies
end