Class: Eventsimple::Message

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/eventsimple/message.rb

Direct Known Subclasses

Metadata

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, type = Dry::Types::Any, **options) ⇒ Object



25
26
27
28
# File 'lib/eventsimple/message.rb', line 25

def self.attribute(name, type = Dry::Types::Any, **options)
  validate_type(type)
  super
end

.attribute?(name, type = Dry::Types::Any, **options) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/eventsimple/message.rb', line 30

def self.attribute?(name, type = Dry::Types::Any, **options)
  validate_type(type)
  super
end

.optionalObject



36
37
38
# File 'lib/eventsimple/message.rb', line 36

def optional
  super.meta(Eventsimple::Types::EVENTSIMPLE_META)
end

.valid_eventsimple_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/eventsimple/message.rb', line 62

def valid_eventsimple_type?(type)
  return true if type.respond_to?(:meta) && type.meta[:eventsimple] == true
  return true if type.is_a?(Class) && type < Eventsimple::Message

  false
end

.validate_type(type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/eventsimple/message.rb', line 40

def validate_type(type)
  return if valid_eventsimple_type?(type)

  source_location = begin
    source = const_source_location(name)&.first if respond_to?(:const_source_location) && name
    source || caller_locations.find { |loc| !loc.path.include?('eventsimple') }&.path
  rescue StandardError
    'unknown'
  end

  message = [
    "Only Eventsimple::Types are allowed in Message attributes. \n",
    "File: #{source_location} \n",
    "Received: #{type.inspect} \n",
    "Use Eventsimple::Types::String, Eventsimple::Types::Integer, etc. \n",
  ].join(' ')

  deprecator = ActiveSupport::Deprecation.new
  deprecator.behavior = Rails.application.config.active_support.deprecation
  deprecator.warn(message)
end

Instance Method Details

#inspectObject



21
22
23
# File 'lib/eventsimple/message.rb', line 21

def inspect
  as_json
end