Class: KDoc::Table

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_doc/table.rb

Overview

Build rows (aka DataTable) with field definitions and rows of data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, data, key = nil, **opts, &block) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
18
19
20
# File 'lib/k_doc/table.rb', line 13

def initialize(parent, data, key = nil, **opts, &block)
  @parent = parent
  @data = data
  @key = (key || FakeOpinion.new.default_table_key).to_s
  @data[@key] = { 'fields' => [], 'rows' => [] }
  @decorators = build_decorators(opts)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



168
169
170
171
172
# File 'lib/k_doc/table.rb', line 168

def method_missing(name, *args, &block)
  return super unless @parent.respond_to?(name)

  @parent.public_send(name, *args, &block)
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/k_doc/table.rb', line 11

def block
  @block
end

#decoratorsObject (readonly)

Returns the value of attribute decorators.



10
11
12
# File 'lib/k_doc/table.rb', line 10

def decorators
  @decorators
end

#keyObject (readonly)

used to be called name



9
10
11
# File 'lib/k_doc/table.rb', line 9

def key
  @key
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/k_doc/table.rb', line 8

def parent
  @parent
end

Instance Method Details

#contextObject



39
40
41
# File 'lib/k_doc/table.rb', line 39

def context
  parent.context
end

#debugObject



136
137
138
# File 'lib/k_doc/table.rb', line 136

def debug
  log.o(KUtil.data.to_open_struct(internal_data))
end

#field(name, *args, default: nil, type: nil) ⇒ Hash Also known as: f

Field definition

Parameters:

  • name (String|Symbol)

    Name of the field

  • args (0)

    Default value if not specified, nil if not set

  • args (1)

    Type of data, string if not set

  • default: (defaults to: nil)

    Default value (using named params), as above

  • type: (defaults to: nil)

    Type of data (using named params), as above

Returns:

  • (Hash)

    Field definition



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/k_doc/table.rb', line 117

def field(name, *args, default: nil, type: nil)
  # default value can be found at position 0 or default: tag (see unit test edge cases)
  default_value = if args.length.positive?
                    args[0].nil? ? default : args[0]
                  else
                    default
                  end

  # type can be found at position 1 or type: tag
  type_value = (args.length > 1 ? args[1] : type) || :string

  {
    'name' => KUtil.data.clean_symbol(name),
    'default' => KUtil.data.clean_symbol(default_value),
    'type' => KUtil.data.clean_symbol(type_value)
  }
end

#fields(*field_definitions) ⇒ Object

Pass fields in using the following format

fields :name, f(:type, :string), :db_type

The older format of an array is supported via a splat conversion



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/k_doc/table.rb', line 47

def fields(*field_definitions)
  field_definitions = *field_definitions[0] if field_definitions.length == 1 && field_definitions[0].is_a?(Array)

  fields = @data[@key]['fields']

  field_definitions.each do |fd|
    fields << if fd.is_a?(String) || fd.is_a?(Symbol)
                field(fd, nil, :string)
              else
                fd
              end
  end

  run_decorators(:update_fields)
end

#find_row(key, value) ⇒ Object



105
106
107
# File 'lib/k_doc/table.rb', line 105

def find_row(key, value)
  @data[@key]['rows'].find { |r| r[key] == value }
end

#fire_evalObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/k_doc/table.rb', line 22

def fire_eval
  return unless block

  instance_eval(&block)

  @has_executed_field_decorators = false
  @has_executed_row_decorators = false

  run_decorators(:update_rows)

  # rubocop:disable Style/RescueStandardError
rescue => e
  # rubocop:enable Style/RescueStandardError
  log.error("Table error for key: #{@key} - #{e.message}")
  raise
end

#get_fieldsObject

rubocop:disable Naming/AccessorMethodName



92
93
94
# File 'lib/k_doc/table.rb', line 92

def get_fields
  @data[@key]['fields']
end

#get_rowsObject



96
97
98
# File 'lib/k_doc/table.rb', line 96

def get_rows
  @data[@key]['rows']
end

#internal_dataObject

rubocop:enable Naming/AccessorMethodName



101
102
103
# File 'lib/k_doc/table.rb', line 101

def internal_data
  @data[@key]
end

#row(*args, **named_args) ⇒ Object

rubocop:disable Metrics/AbcSize

Raises:

  • (KType::Error)


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/k_doc/table.rb', line 64

def row(*args, **named_args)
  fields = @data[@key]['fields']

  raise KType::Error, "To many values for row, argument #{args.length}" if args.length > fields.length

  # Apply column names with defaults

  row = fields.each_with_object({}) do |f, hash|
    hash[f['name']] = f['default']
  end

  # TODO: clean_symbol should be an option that is turned on or off for the table
  # Override with positional arguments
  args.each_with_index do |arg, i|
    row[fields[i]['name']] = arg # KUtil.data.clean_symbol(arg)
  end

  # Override with named args
  named_args.each_key do |arg_name|
    row[arg_name.to_s] = named_args[arg_name] # KUtil.data.clean_symbol(named_args[key])
  end

  @data[@key]['rows'] << row
  row
end