module LolcatStore
LolcatStore — per-channel lolcat rainbow toggle and CLI options. Public: load_state, save_state!, enabled?, set_enabled!, argv, set_argv!, reset! Depends: RabbotDb Tests: test/lib/lolcat_store_test.rb
Constants
- PLUGIN
- STATE_KEY
Public Instance Methods
Source
# File lib/lolcat_store.rb, line 48 def argv(network:, channel:, store: RabbotDb) load_state(network: network, channel: channel, store: store)[:argv] end
Source
# File lib/lolcat_store.rb, line 63 def default_state { enabled: false, argv: [] } end
Source
# File lib/lolcat_store.rb, line 37 def enabled?(network:, channel:, store: RabbotDb) load_state(network: network, channel: channel, store: store)[:enabled] end
Source
# File lib/lolcat_store.rb, line 16 def load_state(network:, channel:, store: RabbotDb) raw = store.get_plugin_json( plugin: PLUGIN, key: STATE_KEY, network: network, channel: channel, default: default_state ) normalize_state(raw) end
Source
# File lib/lolcat_store.rb, line 67 def normalize_state(raw) data = raw.is_a?(Hash) ? raw : {} { enabled: !!data[:enabled] || !!data["enabled"], argv: Array(data[:argv] || data["argv"]).map(&:to_s) } end
Source
# File lib/lolcat_store.rb, line 59 def reset!(network:, channel:, store: RabbotDb) save_state!(default_state, network: network, channel: channel, store: store) end
Source
# File lib/lolcat_store.rb, line 27 def save_state!(state, network:, channel:, store: RabbotDb) store.set_plugin_json( plugin: PLUGIN, key: STATE_KEY, value: normalize_state(state), network: network, channel: channel ) end
Source
# File lib/lolcat_store.rb, line 52 def set_argv!(tokens, network:, channel:, store: RabbotDb) state = load_state(network: network, channel: channel, store: store) state[:argv] = Array(tokens).map(&:to_s) save_state!(state, network: network, channel: channel, store: store) state end
Source
# File lib/lolcat_store.rb, line 41 def set_enabled!(value, network:, channel:, store: RabbotDb) state = load_state(network: network, channel: channel, store: store) state[:enabled] = !!value save_state!(state, network: network, channel: channel, store: store) state end