Class: KDoc::Table
Overview
Build rows (aka DataTable) with field definitions and rows of data
Instance Attribute Summary collapse
-
#decorators ⇒ Object
readonly
Returns the value of attribute decorators.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #debug ⇒ Object
-
#field(name, *args, default: nil, type: nil) ⇒ Hash
(also: #f)
Field definition.
-
#fields(*field_definitions) ⇒ Object
Pass fields in using the following format fields :name, f(:type, :string), :db_type.
- #find_row(key, value) ⇒ Object
-
#get_fields ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #get_rows ⇒ Object
-
#initialize(data, name = nil, **options, &block) ⇒ Table
constructor
A new instance of Table.
-
#internal_data ⇒ Object
rubocop:enable Naming/AccessorMethodName.
-
#row(*args, **named_args) ⇒ Object
rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(data, name = nil, **options, &block) ⇒ Table
Returns a new instance of Table.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/k_doc/table.rb', line 12 def initialize(data, name = nil, **, &block) initialize_attributes(data, name, **) @has_executed_field_decorators = false @has_executed_row_decorators = false instance_eval(&block) if block_given? run_decorators(:update_rows) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
160 161 162 163 164 |
# File 'lib/k_doc/table.rb', line 160 def method_missing(name, *args, &block) return super unless @parent.respond_to?(name) @parent.public_send(name, *args, &block) end |
Instance Attribute Details
#decorators ⇒ Object (readonly)
Returns the value of attribute decorators.
10 11 12 |
# File 'lib/k_doc/table.rb', line 10 def decorators @decorators end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/k_doc/table.rb', line 9 def name @name end |
#parent ⇒ Object (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
#debug ⇒ Object
116 117 118 |
# File 'lib/k_doc/table.rb', line 116 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/k_doc/table.rb', line 97 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
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/k_doc/table.rb', line 27 def fields(*field_definitions) field_definitions = *field_definitions[0] if field_definitions.length == 1 && field_definitions[0].is_a?(Array) fields = @data[@name]['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
85 86 87 |
# File 'lib/k_doc/table.rb', line 85 def find_row(key, value) @data[@name]['rows'].find { |r| r[key] == value } end |
#get_fields ⇒ Object
rubocop:disable Naming/AccessorMethodName
72 73 74 |
# File 'lib/k_doc/table.rb', line 72 def get_fields @data[@name]['fields'] end |
#get_rows ⇒ Object
76 77 78 |
# File 'lib/k_doc/table.rb', line 76 def get_rows @data[@name]['rows'] end |
#internal_data ⇒ Object
rubocop:enable Naming/AccessorMethodName
81 82 83 |
# File 'lib/k_doc/table.rb', line 81 def internal_data @data[@name] end |
#row(*args, **named_args) ⇒ Object
rubocop:disable Metrics/AbcSize
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/k_doc/table.rb', line 44 def row(*args, **named_args) fields = @data[@name]['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 |key| row[key.to_s] = named_args[key] # KUtil.data.clean_symbol(named_args[key]) end @data[@name]['rows'] << row row end |