Class: Mayu::VDOM::Children

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Enumerable, Interfaces::Children
Defined in:
lib/mayu/vdom/children.rb

Constant Summary collapse

Elem =
type_member { { fixed: Interfaces::Descriptor } }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(descriptors, parent_type: nil) ⇒ Children

Returns a new instance of Children.



49
50
51
52
53
54
55
56
# File 'lib/mayu/vdom/children.rb', line 49

def initialize(descriptors, parent_type: nil)
  @descriptors =
    T.let(
      Descriptor::Factory.clean(descriptors, parent_type:),
      T::Array[Interfaces::Descriptor]
    )
  @slots = T.let(nil, T.nilable(Slots))
end

Class Method Details

.check_duplicate_keys(descriptors, parent_type: "??unknown??") ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mayu/vdom/children.rb', line 28

def self.check_duplicate_keys(descriptors, parent_type: "??unknown??")
  keys = descriptors.map(&:key).compact
  duplicates = keys.reject { keys.rindex(_1) == keys.index(_1) }.uniq
  duplicates.each do |key|
    Console.logger.warn(
      self,
      "Duplicate keys detected: #{key.inspect}",
      "This may cause an update error!",
      "Parent type: #{parent_type.inspect}"
    )
  end
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/mayu/vdom/children.rb', line 62

def ==(other)
  case other
  when Children
    @descriptors == other.to_a
  when Array
    @descriptors == other
  else
    false
  end
end

#each(&block) ⇒ Object



112
113
114
# File 'lib/mayu/vdom/children.rb', line 112

def each(&block)
  @descriptors.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


74
# File 'lib/mayu/vdom/children.rb', line 74

def empty? = @descriptors.empty?

#joinObject



102
# File 'lib/mayu/vdom/children.rb', line 102

def join = @descriptors.join

#slot(name = nil, &fallback) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mayu/vdom/children.rb', line 88

def slot(name = nil, &fallback)
  case slots.fetch(name, [])
  in []
    yield if block_given?
  in [one]
    one
  in [*many] unless name
    many
  in [*many]
    raise "Got #{many.size} slots one slot with name #{name.inspect}, #{many.map(&:type).inspect}"
  end
end

#slotsObject



105
# File 'lib/mayu/vdom/children.rb', line 105

def slots = @slots ||= @descriptors.group_by(&:slot)

#to_aObject



59
# File 'lib/mayu/vdom/children.rb', line 59

def to_a = @descriptors