Class: Spree::Event

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
app/models/spree/event.rb

Overview

Represents an event in the Spree event system.

Events are immutable objects that carry information about something that happened in the system. They contain:

  • An id (UUID)

  • A name (e.g., ‘order.complete’, ‘product.create’)

  • A store_id (the store where the event originated)

  • A payload (serialized data about the event)

  • Metadata (contextual information like spree_version)

Examples:

Creating an event

event = Spree::Event.new(
  name: 'order.complete',
  payload: order.serializable_hash
)

Accessing event data

event.id         # => "550e8400-e29b-41d4-a716-446655440000"
event.name       # => 'order.complete'
event.store_id   # => 1
event.payload    # => { 'id' => 1, 'number' => 'R123456' }
event.created_at # => 2024-01-15 10:30:00 UTC

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.matches?(event_name, pattern) ⇒ Boolean

Class method to check if an event name matches a pattern

Parameters:

  • event_name (String)

    The event name

  • pattern (String)

    The pattern (supports * wildcard)

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
# File 'app/models/spree/event.rb', line 87

def self.matches?(event_name, pattern)
  return true if pattern == '*'

  if pattern.include?('*')
    regex = Regexp.new("^#{Regexp.escape(pattern).gsub('\*', '.*')}$")
    event_name.match?(regex)
  else
    event_name == pattern
  end
end

Instance Method Details

#actionString

Returns the action from the event name

Returns:

  • (String)

    The action (e.g., ‘complete’ from ‘order.complete’)



71
72
73
# File 'app/models/spree/event.rb', line 71

def action
  @action ||= name.to_s.split('.').drop(1).join('.')
end

#inspectObject



100
101
102
# File 'app/models/spree/event.rb', line 100

def inspect
  "#<Spree::Event id=#{id.inspect} name=#{name.inspect} store_id=#{store_id.inspect} created_at=#{created_at&.iso8601}>"
end

#matches?(pattern) ⇒ Boolean

Checks if the event matches a pattern Supports wildcards: ‘order.*’ matches ‘order.complete’, ‘order.cancel’

Parameters:

  • pattern (String)

    The pattern to match against

Returns:

  • (Boolean)


79
80
81
# File 'app/models/spree/event.rb', line 79

def matches?(pattern)
  self.class.matches?(name, pattern)
end

#metadata=(value) ⇒ Object



50
51
52
53
# File 'app/models/spree/event.rb', line 50

def metadata=(value)
  base = { 'spree_version' => Spree.version }
  super(base.merge((value || {}).deep_stringify_keys).freeze)
end

#name=(value) ⇒ Object



42
43
44
# File 'app/models/spree/event.rb', line 42

def name=(value)
  super(value.to_s.freeze) if value
end

#payload=(value) ⇒ Object



46
47
48
# File 'app/models/spree/event.rb', line 46

def payload=(value)
  super((value || {}).deep_stringify_keys.freeze)
end

#resource_typeString

Returns the resource type from the event name

Returns:

  • (String)

    The resource type (e.g., ‘order’ from ‘order.complete’)



65
66
67
# File 'app/models/spree/event.rb', line 65

def resource_type
  @resource_type ||= name.to_s.split('.').first
end

#storeSpree::Store?

Returns the store where the event originated

Returns:



57
58
59
60
61
# File 'app/models/spree/event.rb', line 57

def store
  return nil if store_id.blank?

  @store ||= Spree::Store.find_by(id: store_id)
end