Class: Protos::Theme
- Inherits:
-
Object
- Object
- Protos::Theme
- Defined in:
- lib/protos/theme.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
Key can be a symbol or string, they will be merged together for the final css class.
- #add(key, value) ⇒ Object
-
#initialize(theme = {}, tailwind_merge: true, **kwargs) ⇒ Theme
constructor
A new instance of Theme.
- #key?(key) ⇒ Boolean
- #merge(hash) ⇒ Object
- #remove(key, value) ⇒ Object
- #set(key, value) ⇒ Object
Constructor Details
#initialize(theme = {}, tailwind_merge: true, **kwargs) ⇒ Theme
Returns a new instance of Theme.
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/protos/theme.rb', line 19 def initialize(theme = {}, tailwind_merge: true, **kwargs) @should_merge = tailwind_merge @theme = Hash.new do |hash, key| hash[key] = TokenList.new end return if kwargs.empty? && theme.empty? theme.merge!(kwargs).each do |key, value| @theme[key].add(value) end end |
Class Method Details
.merger ⇒ Object
11 12 13 14 15 16 |
# File 'lib/protos/theme.rb', line 11 def merger # This could be a class variable to save memory for subclasses being # used along side this class, but seeing as how its an # internal class I don't see much benefit at the moment. @merger ||= TailwindMerge.new end |
Instance Method Details
#[](*keys) ⇒ Object
Key can be a symbol or string, they will be merged together for the final css class.
-
A symbol will be used to fetch a ‘TokenList` from the theme at that key.
-
A string is used as a plain css value
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/protos/theme.rb', line 37 def [](*keys) symbols, strings = keys.partition { |key| key.instance_of?(Symbol) } values = @theme.values_at(*symbols).map!(&:to_s).reject(&:empty?) values.concat(strings) unless strings.empty? return nil if values.empty? return values unless @should_merge self.class.merger.merge(values) end |
#add(key, value) ⇒ Object
52 53 54 55 56 |
# File 'lib/protos/theme.rb', line 52 def add(key, value) return if value.nil? @theme[key].add(value) end |
#key?(key) ⇒ Boolean
48 49 50 |
# File 'lib/protos/theme.rb', line 48 def key?(key) @theme.key?(key) end |
#merge(hash) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/protos/theme.rb', line 73 def merge(hash) return self unless hash hash.each do |key, value| next add(key, value) if key?(key.to_sym) # Handle negation next remove(key[1..].to_sym, value) if key.start_with?("!") # handle override next set(key[..-2].to_sym, value) if key.end_with?("!") set(key.to_sym, value) end self end |
#remove(key, value) ⇒ Object
58 59 60 61 |
# File 'lib/protos/theme.rb', line 58 def remove(key, value) token_list = @theme[key].remove(value) @theme.delete(key) if token_list.empty? end |
#set(key, value) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/protos/theme.rb', line 63 def set(key, value) return if value.nil? if @theme.key?(key) @theme[key].clear.add(value) else @theme[key].add(value) end end |