Class: SparkComponents::Attributes::Classname

Inherits:
Array
  • Object
show all
Defined in:
lib/spark_components/attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Classname

Returns a new instance of Classname.



49
50
51
52
# File 'lib/spark_components/attributes.rb', line 49

def initialize(*args, &block)
  super(*args, &block)
  @base_set = false
end

Instance Method Details

#add(*args) ⇒ Object



87
88
89
90
# File 'lib/spark_components/attributes.rb', line 87

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

#baseObject



78
79
80
# File 'lib/spark_components/attributes.rb', line 78

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.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spark_components/attributes.rb', line 65

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_class(name, separator = "-") ⇒ Object

Raises:

  • (SparkComponents::Attributes::Error)


104
105
106
107
108
# File 'lib/spark_components/attributes.rb', line 104

def join_class(name, separator = "-")
  raise(SparkComponents::Attributes::Error, "Base class not defined for `join_class(#{name}, …)`") if base.nil?

  [base, name].join(separator)
end

#modifiersObject

Returns clasess which are not defined as a base class



83
84
85
# File 'lib/spark_components/attributes.rb', line 83

def modifiers
  @base_set ? self[1..size] : self
end

#new(*args) ⇒ Object

Easy assess to create a new Attributes::Classname



93
94
95
96
97
98
99
100
101
102
# File 'lib/spark_components/attributes.rb', line 93

def new(*args)
  new_arr = self.class.new

  unless args.empty?
    new_arr.base = args.shift
    new_arr.add(*args)
  end

  new_arr
end

#to_sObject



110
111
112
# File 'lib/spark_components/attributes.rb', line 110

def to_s
  join(" ")
end