Sha256: b3b0c7f85456be5b61d0895024666238676517683d4fbbcb335a7f1ffe87484d

Contents?: true

Size: 781 Bytes

Versions: 2

Compression:

Stored size: 781 Bytes

Contents

# frozen_string_literal: true

class Lux::Page::Flash

  # flash.info 'messsage ...'
  # flash.info = 'messsage ...'
  def self.add_type name
    define_method(name) { |message| add name, message }
    eval "alias #{name}= #{name}"
  end

  add_type :info
  add_type :error
  add_type :warning

  ###

  def initialize h=nil
    @msg = h || {}
  end

  def clear
    to_h.tap { @msg = {} }
  end

  def to_h
    @msg
  end

  # clears white space, replaces quotes
  def clear_for_js
    {}.tap do |msg|
      clear.each do |k, v|
        msg[k] = v.join(', ').gsub(/\s+/, ' ')
      end
    end
  end

  private

  def add name, message
    @msg[name] ||= []

    return if @msg[name].last == message
    return if @msg[name].length > 4

    @msg[name].push message.to_s
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lux-fw-0.1.35 ./lib/lux/page/lib/flash.rb
lux-fw-0.1.17 ./lib/lux/page/lib/flash.rb