Class: FlashMessenger::Storage
- Inherits:
-
Object
- Object
- FlashMessenger::Storage
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Model, ActiveModel::Serializers::JSON
- Defined in:
- lib/flash_messenger/storage.rb
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#messages ⇒ Object
Returns the value of attribute messages.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(message) ⇒ Object
- #add(klass, level, msg, **i18n_params) ⇒ Object
- #alert(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
-
#alerts ⇒ Object
Message Helpers (by level).
- #attributes ⇒ Object
- #attributes=(hash) ⇒ Object
- #delete(message_or_translation) ⇒ Object
- #each_message(&block) ⇒ Object
- #error(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
- #errors ⇒ Object
-
#from_json(json, include_root = include_root_in_json) ⇒ Object
Serialization Helpers.
- #info(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
- #infos ⇒ Object
-
#initialize(locale = DEFAULT_LOCALE.to_sym) ⇒ Storage
constructor
A new instance of Storage.
- #notice(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
- #notices ⇒ Object
- #serializable_hash ⇒ Object
- #to_session ⇒ Object
Constructor Details
#initialize(locale = DEFAULT_LOCALE.to_sym) ⇒ Storage
Returns a new instance of Storage.
10 11 12 |
# File 'lib/flash_messenger/storage.rb', line 10 def initialize(locale = DEFAULT_LOCALE.to_sym) @locale, = locale.to_sym, [] end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
8 9 10 |
# File 'lib/flash_messenger/storage.rb', line 8 def locale @locale end |
#messages ⇒ Object
Returns the value of attribute messages.
8 9 10 |
# File 'lib/flash_messenger/storage.rb', line 8 def end |
Class Method Details
.from_session(json) ⇒ Object
103 104 105 |
# File 'lib/flash_messenger/storage.rb', line 103 def self.from_session(json) new.from_json(json, false) end |
Instance Method Details
#<<(message) ⇒ Object
14 15 16 17 18 |
# File 'lib/flash_messenger/storage.rb', line 14 def <<() return unless .is_a?(Messages::Base) return if .any? { |i| .eql?(i) } << end |
#add(klass, level, msg, **i18n_params) ⇒ Object
75 76 77 78 79 |
# File 'lib/flash_messenger/storage.rb', line 75 def add(klass, level, msg, **i18n_params) classify(klass).send(level, msg, **i18n_params).tap do |instance| self.<< instance end end |
#alert(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
59 60 61 |
# File 'lib/flash_messenger/storage.rb', line 59 def alert(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) add(klass, :alert, msg, **i18n_params) end |
#alerts ⇒ Object
Message Helpers (by level)
43 44 45 |
# File 'lib/flash_messenger/storage.rb', line 43 def alerts .select(&:alert?) end |
#attributes ⇒ Object
121 122 123 |
# File 'lib/flash_messenger/storage.rb', line 121 def attributes instance_values end |
#attributes=(hash) ⇒ Object
115 116 117 118 119 |
# File 'lib/flash_messenger/storage.rb', line 115 def attributes=(hash) hash.each do |key, value| send("#{key}=", value) end end |
#delete(message_or_translation) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/flash_messenger/storage.rb', line 31 def delete() matcher = /#{message_or_translation}/ .delete_if do || .to_s =~ matcher || .translation =~ matcher end end |
#each_message(&block) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/flash_messenger/storage.rb', line 20 def (&block) = [] I18n.with_locale(@locale) do .each do || block.call() if block_given? << unless .persistent? end end .each { |i| .delete(i) } end |
#error(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
63 64 65 |
# File 'lib/flash_messenger/storage.rb', line 63 def error(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) add(klass, :error, msg, **i18n_params) end |
#errors ⇒ Object
47 48 49 |
# File 'lib/flash_messenger/storage.rb', line 47 def errors .select(&:error?) end |
#from_json(json, include_root = include_root_in_json) ⇒ Object
Serialization Helpers
85 86 87 88 89 90 91 |
# File 'lib/flash_messenger/storage.rb', line 85 def from_json(json, include_root = include_root_in_json) super.tap do |instance| instance. = instance..collect do |m| m.is_a?(Hash) ? classify(m['class']).from_session(m.to_json) : m end end end |
#info(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
67 68 69 |
# File 'lib/flash_messenger/storage.rb', line 67 def info(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) add(klass, :info, msg, **i18n_params) end |
#infos ⇒ Object
55 56 57 |
# File 'lib/flash_messenger/storage.rb', line 55 def infos .select(&:info?) end |
#notice(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) ⇒ Object
71 72 73 |
# File 'lib/flash_messenger/storage.rb', line 71 def notice(msg, klass = DEFAULT_MESSAGE_KLASS, **i18n_params) add(klass, :notice, msg, **i18n_params) end |
#notices ⇒ Object
51 52 53 |
# File 'lib/flash_messenger/storage.rb', line 51 def notices .select(&:notice?) end |
#serializable_hash ⇒ Object
93 94 95 96 97 |
# File 'lib/flash_messenger/storage.rb', line 93 def serializable_hash super.tap do |instance| instance['messages'].map!(&:serializable_hash) end end |
#to_session ⇒ Object
99 100 101 |
# File 'lib/flash_messenger/storage.rb', line 99 def to_session serializable_hash.to_json end |