module ThemeStore
ThemeStore — active theme and custom theme persistence per channel. Public: active, set_active!, custom_themes, save_custom!, last_generated, set_last_generated! Depends: RabbotDb Tests: test/lib/theme_store_test.rb
Constants
- ACTIVE_KEY
- CUSTOM_KEY
- LAST_GENERATED_KEY
- PLUGIN
Public Instance Methods
Source
# File lib/theme_store.rb, line 18 def active(network:, channel:, store: RabbotDb) store.get_plugin_json( plugin: PLUGIN, key: ACTIVE_KEY, network: network, channel: channel, default: nil ) end
Source
# File lib/theme_store.rb, line 39 def custom_themes(network:, channel:, store: RabbotDb) raw = store.get_plugin_json( plugin: PLUGIN, key: CUSTOM_KEY, network: network, channel: channel, default: [] ) Array(raw).map { |entry| symbolize_theme(entry) } end
Source
# File lib/theme_store.rb, line 65 def find_custom(id, network:, channel:, store: RabbotDb) custom_themes(network: network, channel: channel, store: store).find do |entry| entry[:id].to_s == id.to_s end end
Source
# File lib/theme_store.rb, line 71 def last_generated(network:, channel:, store: RabbotDb) raw = store.get_plugin_json( plugin: PLUGIN, key: LAST_GENERATED_KEY, network: network, channel: channel, default: nil ) raw.nil? ? nil : symbolize_theme(raw) end
Source
# File lib/theme_store.rb, line 50 def save_custom!(theme, network:, channel:, store: RabbotDb) themes = custom_themes(network: network, channel: channel, store: store) normalized = symbolize_theme(theme) themes.reject! { |entry| entry[:id] == normalized[:id] } themes << normalized store.set_plugin_json( plugin: PLUGIN, key: CUSTOM_KEY, value: themes, network: network, channel: channel ) normalized end
Source
# File lib/theme_store.rb, line 28 def set_active!(theme_id, network:, channel:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: ACTIVE_KEY, value: theme_id.to_s, network: network, channel: channel ) theme_id.to_s end
Source
# File lib/theme_store.rb, line 82 def set_last_generated!(theme, network:, channel:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: LAST_GENERATED_KEY, value: symbolize_theme(theme), network: network, channel: channel ) end
Source
# File lib/theme_store.rb, line 116 def symbolize_frame(raw) return {} unless raw.is_a?(Hash) h = raw.transform_keys(&:to_sym) { header_doodle: Array(h[:header_doodle] || h["header_doodle"]).map(&:to_s), footer_doodle: Array(h[:footer_doodle] || h["footer_doodle"]).map(&:to_s), separator: (h[:separator] || h["separator"]).to_s } end
Source
# File lib/theme_store.rb, line 128 def symbolize_paint_rules(raw) Array(raw).map do |rule| h = rule.is_a?(Hash) ? rule.transform_keys(&:to_sym) : {} entry = { plugin: (h[:plugin] || h["plugin"]).to_s, line: (h[:line] || h["line"]).to_i, pos: (h[:pos] || h["pos"]).to_i, fg: (h[:fg] || h["fg"]).to_i } bg = h[:bg] || h["bg"] entry[:bg] = bg.to_i if bg entry end end
Source
# File lib/theme_store.rb, line 107 def symbolize_palette(raw) return {} unless raw.is_a?(Hash) raw.each_with_object({}) do |(key, value), out| out[key.to_sym] = Array(value).map(&:to_i) end end
Source
# File lib/theme_store.rb, line 92 def symbolize_theme(entry) return entry if entry.is_a?(Hash) && entry.key?(:id) && entry[:id].is_a?(String) h = entry.is_a?(Hash) ? entry.transform_keys(&:to_sym) : {} { id: (h[:id] || h["id"]).to_s, name: (h[:name] || h["name"]).to_s, width: (h[:width] || h["width"])&.to_sym, palette: symbolize_palette(h[:palette] || h["palette"]), frame: symbolize_frame(h[:frame] || h["frame"]), paint_rules: symbolize_paint_rules(h[:paint_rules] || h["paint_rules"]) } end