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
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  return nil unless key?(key)

  value = @theme[key].to_s
  return nil if value.empty?
  return value unless @tailwind_merge

  self.class.merger.merge(value)
end

#add(key, value) ⇒ Object



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

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

  @theme[key].add(value)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#merge(hash) ⇒ Object



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

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



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

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

#set(key, value) ⇒ Object



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

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

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