class YeelightCli::BulbGroup

Bulbs container

Attributes

items[RW]
name[RW]

Public Class Methods

new(name:, includes: []) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 9
def initialize(name:, includes: [])
  @name = name
  @items = []

  self << includes
end

Public Instance Methods

<<(elements)
Alias for: add_items
<=>(other) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 20
def <=>(other)
  name <=> other.name
end
==(other) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 16
def ==(other)
  name == other.name && items == other.items
end
[](name)
Alias for: subgroup
add_items(elements) click to toggle source

rubocop:enable MethodMissingSuper

# File lib/yeelight_cli/bulb_group.rb, line 44
def add_items(elements)
  @items += Array.wrap(elements)
end
Also aliased as: <<
each() { |item| ... } click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 24
def each(&block)
  @items.each do |item|
    next item.each(&block) if item.is_a?(self.class)

    yield item
  end
end
find_in_subgroups(names) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 53
def find_in_subgroups(names)
  casted_names = names.map(&:to_s)
  subgroups.select { |item| item.name.to_s.in?(casted_names) }
end
find_lamps(identifiers) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 58
def find_lamps(identifiers)
  casted_identifiers = Array.wrap(identifiers).map(&:to_s)

  select do |item|
    item.name.to_s.in?(casted_identifiers) ||
      item.id.to_s.in?(casted_identifiers)
  end
end
method_missing(method_name, *args, &block) click to toggle source

rubocop:disable MethodMissingSuper

# File lib/yeelight_cli/bulb_group.rb, line 37
def method_missing(method_name, *args, &block)
  @items.map do |item|
    item.send(method_name, *args, &block)
  end.flatten
end
respond_to_missing?(method_name, include_priv) click to toggle source
Calls superclass method
# File lib/yeelight_cli/bulb_group.rb, line 32
def respond_to_missing?(method_name, include_priv)
  YeelightCli::Bulb.instance_methods.include?(method_name) || super
end
subgroup(name) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 67
def subgroup(name)
  @items.find { |item| item.name.to_s == name.to_s }
end
Also aliased as: []
subgroup_names() click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 72
def subgroup_names
  @items.map(&:name)
end
subgroups() click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 49
def subgroups
  @items.select { |item| item.class == self.class }
end
to_graph(bulb_group: self, deep_level: 0, squash: false) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 80
def to_graph(bulb_group: self, deep_level: 0, squash: false)
  graph = draw_bulb_group(bulb_group, squash, deep_level)

  bulb_group.items.group_by(&:class).tap do |grouped_items|
    child_groups = grouped_items[self.class].try(:sort)
    child_bulbs = grouped_items[Bulb].try(:sort)

    graph << draw_bulbs(child_bulbs, deep_level, squash) if child_bulbs

    if child_groups
      graph << draw_bulb_groups(child_groups, squash, deep_level + 1)
    end
  end

  graph
end
to_icons() click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 76
def to_icons
  map(&:to_icon).join
end

Private Instance Methods

draw_bulb_group(bulb_group, squash, deep_level = 0) click to toggle source

rubocop:disable AbcSize

# File lib/yeelight_cli/bulb_group.rb, line 110
def draw_bulb_group(bulb_group, squash, deep_level = 0)
  result = ''

  if deep_level.positive?
    result << '    ' * (deep_level - 1) + '|' + "\n" unless squash
    result << '    ' * (deep_level - 1) + '|' + '--'
  end

  result + bulb_group.name.to_s + "\n"
end
draw_bulb_groups(bulb_groups, squash, deep_level) click to toggle source
# File lib/yeelight_cli/bulb_group.rb, line 99
def draw_bulb_groups(bulb_groups, squash, deep_level)
  bulb_groups.map do |bulb_group|
    to_graph(
      bulb_group: bulb_group,
      deep_level: deep_level,
      squash: squash
    )
  end.join
end
draw_bulbs(bulbs, deep_level, squash) click to toggle source

rubocop:enable AbcSize

# File lib/yeelight_cli/bulb_group.rb, line 122
def draw_bulbs(bulbs, deep_level, squash)
  bulb_icons = bulbs.map(&:to_icon)
  result = ''
  result = '   ' * deep_level + '  ' + '|' + "\n" unless squash
  result + '   ' * deep_level + '  ' + bulb_icons.join + "\n"
end