Class: Spark::Component::Classname

Inherits:
Array
  • Object
show all
Defined in:
lib/spark/component/classname.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, base: nil, &block) ⇒ Classname

Returns a new instance of Classname.



6
7
8
9
10
# File 'lib/spark/component/classname.rb', line 6

def initialize(*args, base: nil, &block)
  super(args, &block)
  self.base = base unless base.nil?
  @base_set = !base.nil?
end

Instance Method Details

#add(*args) ⇒ Object



40
41
42
43
# File 'lib/spark/component/classname.rb', line 40

def add(*args)
  push(*args.flatten.uniq.reject { |a| a.nil? || include?(a) })
  self
end

#baseObject



36
37
38
# File 'lib/spark/component/classname.rb', line 36

def base
  first if @base_set
end

#base=(klass) ⇒ Object

Many elements have a base class which defines core utlitiy This classname may serve as a root for other element classnames and should be distincly accessible

For example:

classes = Classname.new
classes.base = 'nav__item'
now generate a wrapper: "#{classes.base}__wrapper"

Ensure base class is the first element in the classes array.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spark/component/classname.rb', line 23

def base=(klass)
  return if klass.nil? || klass.empty?

  if @base_set
    self[0] = klass
  else
    unshift klass
    @base_set = true
  end

  uniq!
end

#join_base(name, separator = "-") ⇒ Object

Raises:

  • (StandardError)


45
46
47
48
49
# File 'lib/spark/component/classname.rb', line 45

def join_base(name, separator = "-")
  raise(StandardError, "Base class not defined for `join_base(#{name}, …)`") if base.nil?

  [base, name].join(separator)
end

#to_sObject



51
52
53
# File 'lib/spark/component/classname.rb', line 51

def to_s
  join(" ")
end