Class: Docp::TableHeader

Inherits:
Object
  • Object
show all
Includes:
TableHeaderPtn
Defined in:
lib/docp/table_header.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Attributes included from TableHeaderPtn

#after_check_val, #exclude_ptn, #include_ptn

Instance Method Summary collapse

Methods included from TableHeaderPtn

#check_ptn, #exclude_ptn?, #ptn?

Constructor Details

#initialize(match = nil, args = {}, &block) ⇒ TableHeader

Returns a new instance of TableHeader.



21
22
23
24
25
26
27
28
29
# File 'lib/docp/table_header.rb', line 21

def initialize match = nil, args = {}, &block
  @columns = []
  @required_columns = []
  @required_attributes = []
  args.each {|k, v| send("#{k}=", v) }
  @default_format ||= :text
  block.call(self, match)
  set_required_columns
end

Instance Attribute Details

#after_to_hashObject

Returns the value of attribute after_to_hash.



17
18
19
# File 'lib/docp/table_header.rb', line 17

def after_to_hash
  @after_to_hash
end

#before_parseObject

TableOption



12
13
14
# File 'lib/docp/table_header.rb', line 12

def before_parse
  @before_parse
end

#childObject (readonly)

Returns the value of attribute child.



8
9
10
# File 'lib/docp/table_header.rb', line 8

def child
  @child
end

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/docp/table_header.rb', line 6

def columns
  @columns
end

#default_formatObject

RowOption



16
17
18
# File 'lib/docp/table_header.rb', line 16

def default_format
  @default_format
end

#match_block(&block) ⇒ Object (readonly)

Returns the value of attribute match_block.



7
8
9
# File 'lib/docp/table_header.rb', line 7

def match_block
  @match_block
end

#required_attributesObject Also known as: required_keys

Returns the value of attribute required_attributes.



9
10
11
# File 'lib/docp/table_header.rb', line 9

def required_attributes
  @required_attributes
end

#verticalObject

Returns the value of attribute vertical.



13
14
15
# File 'lib/docp/table_header.rb', line 13

def vertical
  @vertical
end

Instance Method Details

#[](name) ⇒ Object



51
52
53
54
# File 'lib/docp/table_header.rb', line 51

def [] name
  name = name.to_sym if name.is_a?(String)
  @columns.find {|col| col.name == name}
end

#add(h) {|col| ... } ⇒ Object

Yields:

  • (col)


95
96
97
98
99
100
# File 'lib/docp/table_header.rb', line 95

def add h
  col = Column.new(h.merge(default_format: @default_format))
  yield col if block_given?
  @columns.push(*[col, col.children].flatten)
  col
end

#include_ptn?(tr) ⇒ Boolean

if match_block CreateSelfInstance & Schema == Child

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/docp/table_header.rb', line 37

def include_ptn? tr
  [@include_ptn || @columns.map(&:include_ptn)].flatten.map.with_index do|ptn, i|
    if ptn?(tr.search('*'), ptn)
      match = {ptn: ptn, index: i, tr: tr}
      @child = TableHeader.new match, &@match_block if @match_block
      if @child
        return !@child.exclude_ptn?(tr.search('*'))
      else
        return true
      end
    end
  end.any?
end

#no_hash_keysObject



91
92
93
# File 'lib/docp/table_header.rb', line 91

def no_hash_keys
  @columns.select(&:no_hash).map(&:name)
end

#required_all?(tr) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/docp/table_header.rb', line 78

def required_all? tr
  return true if required_columns.empty?
  cols = required_columns.select {|col| col.include_ptn?(tr.row_elements) }
  if cols.count >= required_keys.count
    true
  else
    keys = required_keys.dup
    cols.each {|col| keys.delete(col.name)}
    #header_required_undefineds = { keys: keys, tr: tr.clone }
    nil
  end
end

#required_columnsObject



74
75
76
# File 'lib/docp/table_header.rb', line 74

def required_columns
  @columns.select {|col| col.required}
end

#set_required_columnsObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/docp/table_header.rb', line 56

def set_required_columns
  #keys = @required_attributes.select {|name| [email protected] {|col| col.name == name} }
  #raise "Column NotFound #{keys}" if keys.any?
  if @required_attributes.any?
    @columns.select do|col|
      f = @required_attributes.find {|name| col.name == name}
      f ? col.required = true : col.required = false
    end
  else
    required_columns.map {|col| col.required = false }
  end
end

#swap(h) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/docp/table_header.rb', line 102

def swap h
  col = Column.new(h.merge(default_format: @default_format))
  if i = @columns.index {|ch| ch.name == col.name}
    @columns[i] = col
  else
    raise ArgumentError, "#{col.name} ColumnNotFound"
  end
end