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/tr.rb,
lib/deep/hash/struct/dashboard/table/row.rb

Defined Under Namespace

Classes: Row, Tr

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PP::Dashboard::Table

#inspect, #pretty_print

Constructor Details

#initialize(options = {}) ⇒ Table



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

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.



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

def bodies
  @bodies
end

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#optObject

Returns the value of attribute opt.



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

def opt
  @opt
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#sideObject

Returns the value of attribute side.



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

def side
  @side
end

#sidesObject

Returns the value of attribute sides.



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

def sides
  @sides
end

Instance Method Details

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

Yields:



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

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

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

Yields:



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

def add_header
  yield header
end

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

Yields:



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

def add_side
  yield side
end

#eachObject



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

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

#mapObject Also known as: collect



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

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

#matrix?Boolean



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

def matrix?
  opt[:matrix]
end

#parse!Object



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
87
88
# File 'lib/deep/hash/struct/dashboard/table.rb', line 47

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?
                  bs = Wrapper.new

                  body.size.times do
                    bs.deep_merge! body.shift
                  end

                  sides.map do |s|
                    rs  = []
                    rs << s
                    rs += headers.map do |h|
                      next if h.side_header?
                      v = bs.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

#tr(options = {}) {|Tr.new(header, b, side, opt)| ... } ⇒ Object

Yields:



111
112
113
114
115
116
117
118
119
120
# File 'lib/deep/hash/struct/dashboard/table.rb', line 111

def tr(options = {})
  b             = Wrapper.new
  opt[:side]    = options[:side] || false
  opt[:matrix]  = matrix?
  opt[:b_index] =  body.size

  yield Tr.new(header, b, side, opt)
  body << b unless b.count.zero?
  self
end