Class: HakumiComponents::Table::RowRecord

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/table/row_record.rb

Constant Summary collapse

Source =

T.untyped is intentional: table rows can be ActiveRecord models, plain Hashes, OpenStructs, or any PORO. RowRecord is the type-safe adapter that converts this external boundary into typed method calls.

T.type_alias { T.untyped }
RowKeyConfig =
T.type_alias { T.any(Types::StringOrSymbol, Proc) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, children_column_name:) ⇒ RowRecord

Returns a new instance of RowRecord.



16
17
18
19
# File 'app/components/hakumi_components/table/row_record.rb', line 16

def initialize(source:, children_column_name:)
  @source = T.let(source, Source)
  @children_column_name = T.let(children_column_name, Types::StringOrSymbol)
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



22
23
24
# File 'app/components/hakumi_components/table/row_record.rb', line 22

def source
  @source
end

Instance Method Details

#childrenObject



50
51
52
53
54
55
56
57
# File 'app/components/hakumi_components/table/row_record.rb', line 50

def children
  children_value = value(@children_column_name)
  return [] unless children_value.is_a?(Array)

  children_value.map do |child|
    HakumiComponents::Table::RowRecord.new(source: child, children_column_name: @children_column_name)
  end
end

#key_for(row_key, index) ⇒ Object



41
42
43
44
45
46
47
# File 'app/components/hakumi_components/table/row_record.rb', line 41

def key_for(row_key, index)
  if row_key.is_a?(Proc)
    row_key.arity == 1 ? row_key.call(@source) : row_key.call(@source, index)
  else
    value(row_key)
  end
end

#value(data_index) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/components/hakumi_components/table/row_record.rb', line 25

def value(data_index)
  return nil if data_index.nil?

  if @source.is_a?(Hash)
    return @source[data_index] if @source.key?(data_index)

    string_key = data_index.to_s
    return @source[string_key] if @source.key?(string_key)
  end

  return @source.public_send(data_index) if @source&.respond_to?(data_index)

  nil
end