Class: Spree::Event
- Inherits:
-
Object
- Object
- Spree::Event
- 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)
Class Method Summary collapse
-
.matches?(event_name, pattern) ⇒ Boolean
Class method to check if an event name matches a pattern.
Instance Method Summary collapse
-
#action ⇒ String
Returns the action from the event name.
- #inspect ⇒ Object
-
#matches?(pattern) ⇒ Boolean
Checks if the event matches a pattern Supports wildcards: ‘order.*’ matches ‘order.complete’, ‘order.cancel’.
- #metadata=(value) ⇒ Object
- #name=(value) ⇒ Object
- #payload=(value) ⇒ Object
-
#resource_type ⇒ String
Returns the resource type from the event name.
-
#store ⇒ Spree::Store?
Returns the store where the event originated.
Class Method Details
.matches?(event_name, pattern) ⇒ Boolean
Class method to check if an event name matches a pattern
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
#action ⇒ String
Returns the action from the event name
71 72 73 |
# File 'app/models/spree/event.rb', line 71 def action @action ||= name.to_s.split('.').drop(1).join('.') end |
#inspect ⇒ Object
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’
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 (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_type ⇒ String
Returns the resource type from the event name
65 66 67 |
# File 'app/models/spree/event.rb', line 65 def resource_type @resource_type ||= name.to_s.split('.').first end |
#store ⇒ Spree::Store?
Returns the store where the event originated
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 |