Class: Steroids::Support::NoticableMethods::NoticableCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/steroids/support/noticable_methods.rb

Overview


Noticable collection


Constant Summary collapse

NOTICABLE_TYPES =
i[errors notices]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_type) ⇒ NoticableCollection

Returns a new instance of NoticableCollection.



30
31
32
33
# File 'lib/steroids/support/noticable_methods.rb', line 30

def initialize(collection_type)
  @collection_type = NOTICABLE_TYPES.cast(collection_type)
  @collection = []
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



26
27
28
# File 'lib/steroids/support/noticable_methods.rb', line 26

def collection
  @collection
end

Instance Method Details

#add(message, exception = nil) ⇒ Object Also known as: <<



35
36
37
38
39
40
41
42
# File 'lib/steroids/support/noticable_methods.rb', line 35

def add(message, exception = nil)
  nil.tap do
    @collection << {
      message: message.typed!(String),
      exception: exception
    }
  end
end

#full_messagesObject



54
55
56
57
58
59
60
# File 'lib/steroids/support/noticable_methods.rb', line 54

def full_messages
  if @collection.any?
    @collection.map do |error|
      error[:message]
    end.join("\n").presence
  end
end

#merge(errors) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/steroids/support/noticable_methods.rb', line 44

def merge(errors)
  nil.tap do
    errors.each do |error|
      @collection << error
    end
  end
end