Class: HakumiComponents::Skeleton::ParagraphConfig

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

Constant Summary collapse

WidthValue =
T.type_alias { T.any(Integer, String) }
WidthSource =
T.type_alias { T.nilable(T.any(WidthValue, T::Array[WidthValue])) }
Input =
T.type_alias { T.any(T::Boolean, Types::HtmlAttributes, HakumiComponents::Skeleton::ParagraphConfig) }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows: 3, width_source: nil) ⇒ ParagraphConfig

Returns a new instance of ParagraphConfig.



14
15
16
17
18
# File 'app/components/hakumi_components/skeleton/paragraph_config.rb', line 14

def initialize(rows: 3, width_source: nil)
  @rows = T.let(rows, Integer)
  @width_source = T.let(width_source, WidthSource)
  validate!
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



21
22
23
# File 'app/components/hakumi_components/skeleton/paragraph_config.rb', line 21

def rows
  @rows
end

Class Method Details

.coerce(value) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'app/components/hakumi_components/skeleton/paragraph_config.rb', line 41

def self.coerce(value)
  return nil if value == false
  return new if value == true
  return value if value.is_a?(HakumiComponents::Skeleton::ParagraphConfig)

  rows = parse_rows(value[:rows])
  widths = parse_width_source(value[:widths], value[:width])
  new(rows: rows || 3, width_source: widths)
end

Instance Method Details

#row_widths(default_rows) ⇒ Object



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

def row_widths(default_rows)
  rows = @rows.positive? ? @rows : default_rows
  resolved = case @width_source
  when Array
    @width_source
  when nil
    default_widths(rows)
  else
    Array.new(rows, @width_source)
  end

  normalized = resolved.first(rows)
  normalized.fill("100%", normalized.length...rows)
  normalized
end