class Styler::Collection

Attributes

collection_names[R]
name[R]
parent[R]
style_names[R]

Public Class Methods

new(parent = nil, name = :default) click to toggle source
Calls superclass method
# File lib/styler/collection.rb, line 4
def initialize(parent = nil, name = :default)
  super(parent) if parent
  @parent = parent
  @name = name
  @style_names = []
  @collection_names = []
end

Public Instance Methods

==(other) click to toggle source
# File lib/styler/collection.rb, line 12
def ==(other)
  self.class == other.class && name == other.name
end
collection(name, &block) click to toggle source
# File lib/styler/collection.rb, line 31
def collection(name, &block)
  @collection_names << name

  define_singleton_method(name) do |*args|
    self.class.new(self, name).tap do |c|
      c.instance_exec(*args, &block)
    end
  end
end
collection_alias(name, collection = nil, &block) click to toggle source
# File lib/styler/collection.rb, line 41
def collection_alias(name, collection = nil, &block)
  @collection_names << name

  define_singleton_method(name) do |*args|
    if collection
      collection
    else
      instance_exec(*args, &block)
    end
  end
end
collections() click to toggle source
# File lib/styler/collection.rb, line 76
def collections
  collection_names.map { |name| send(name) }
end
copy_styles_from(collection:) click to toggle source
# File lib/styler/collection.rb, line 53
def copy_styles_from(collection:)
  collection.styles.each do |style|
    style(style.name, style.to_a)
  end
end
nested_collections() click to toggle source
# File lib/styler/collection.rb, line 84
def nested_collections
  if collections.empty?
    []
  else
    collections + collections.flat_map(&:nested_collections)
  end
end
nested_styles() click to toggle source
# File lib/styler/collection.rb, line 80
def nested_styles
  styles + nested_collections.flat_map(&:styles)
end
path() click to toggle source
# File lib/styler/collection.rb, line 59
def path
  return "root" if parent.nil?
  "#{parent.path}.#{name}"
end
repeted_styles() click to toggle source
# File lib/styler/collection.rb, line 64
def repeted_styles
  nested_styles
    .group_by(&:to_a)
    .select { |_, styles| styles.count > 1 }
    .to_h
    .values
end
style(name, styles = nil, &block) click to toggle source
# File lib/styler/collection.rb, line 16
def style(name, styles = nil, &block)
  @style_names << name

  if styles
    define_singleton_method(name) do |*args|
      Styler::Style.new(self, name, styles)
    end
  else
    define_singleton_method(name) do |*args|
      styles = instance_exec(*args, &block)
      Styler::Style.new(self, name, styles)
    end
  end
end
styles() click to toggle source
# File lib/styler/collection.rb, line 72
def styles
  style_names.map { |name| send(name) }
end