Class: Protos::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/protos/theme.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme = {}, tailwind_merge: true, **kwargs) ⇒ Theme

Returns a new instance of Theme.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/protos/theme.rb', line 16

def initialize(theme = {}, tailwind_merge: true, **kwargs)
  @tailwind_merge = tailwind_merge

  @theme = Hash.new do |hash, key|
    hash[key] = TokenList.new
  end

  theme.merge!(kwargs).each do |key, value|
    @theme[key].add(value)
  end
end

Class Method Details

.mergerObject



11
12
13
# File 'lib/protos/theme.rb', line 11

def merger
  @merger ||= TailwindMerge::Merger.new.freeze
end

Instance Method Details

#[](*keys) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/protos/theme.rb', line 28

def [](*keys)
  symbols, strings = keys.partition { |key| key.is_a?(Symbol) }
  values = @theme.values_at(*symbols).join(" ")
  values += " #{strings.join(" ")}" unless strings.empty?

  return nil if values.empty?
  return values unless @tailwind_merge

  self.class.merger.merge(values)
end

#add(key, value) ⇒ Object



43
44
45
46
47
# File 'lib/protos/theme.rb', line 43

def add(key, value)
  return if value.nil?

  @theme[key].add(value)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/protos/theme.rb', line 39

def key?(key)
  @theme.key?(key)
end

#merge(hash) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/protos/theme.rb', line 60

def merge(hash)
  return self unless hash

  hash.each do |key, value|
    if key?(key)
      add(key, value)
    elsif negation?(key)
      remove(key[1..].to_sym, value)
    elsif override?(key)
      set(key[..-2].to_sym, value)
    else
      set(key, value)
    end
  end

  self
end

#remove(key, value) ⇒ Object



49
50
51
52
# File 'lib/protos/theme.rb', line 49

def remove(key, value)
  @theme[key].remove(value)
  @theme.delete(key) if @theme[key].empty?
end

#set(key, value) ⇒ Object



54
55
56
57
58
# File 'lib/protos/theme.rb', line 54

def set(key, value)
  return if value.nil?

  @theme[key].clear.add(value)
end