Class: Stance::Event
- Inherits:
-
Object
- Object
- Stance::Event
- Includes:
- ActiveSupport::Callbacks
- Defined in:
- lib/stance/event.rb
Class Attribute Summary collapse
-
.callback_methods ⇒ Object
Returns the value of attribute callback_methods.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Class Method Summary collapse
- .after_create(*methods, &block) ⇒ Object
- .before_create(*methods, &block) ⇒ Object
- .method_added(method_name) ⇒ Object
Instance Method Summary collapse
- #create ⇒ Object
- #full_name ⇒ Object
-
#initialize(name, subject, metadata, options) ⇒ Event
constructor
A new instance of Event.
- #subject ⇒ Object
Constructor Details
#initialize(name, subject, metadata, options) ⇒ Event
Returns a new instance of Event.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/stance/event.rb', line 31 def initialize(name, subject, , ) @subject = subject @name = name @metadata = @options = { singleton: false, record: true, class: false }.merge() attrs = { name: name, metadata: } if subject.is_a?(ActiveRecord::Base) attrs[:subject] = subject elsif subject.is_a?(Class) && subject < ActiveRecord::Base attrs[:subject_type] = subject.name else @options[:record] = false end return unless @options[:record] @record = Stance::EventRecord.new(attrs) end |
Class Attribute Details
.callback_methods ⇒ Object
Returns the value of attribute callback_methods.
11 12 13 |
# File 'lib/stance/event.rb', line 11 def callback_methods @callback_methods end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/stance/event.rb', line 8 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/stance/event.rb', line 8 def @options end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
8 9 10 |
# File 'lib/stance/event.rb', line 8 def record @record end |
Class Method Details
.after_create(*methods, &block) ⇒ Object
17 18 19 |
# File 'lib/stance/event.rb', line 17 def after_create(*methods, &block) set_callback :create, :after, *methods, &block end |
.before_create(*methods, &block) ⇒ Object
13 14 15 |
# File 'lib/stance/event.rb', line 13 def before_create(*methods, &block) set_callback :create, :before, *methods, &block end |
Instance Method Details
#create ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/stance/event.rb', line 51 def create return self if singleton_exists? Stance::EventRecord.transaction do run_callbacks :create do # Call each public method of the Event class if a custom class. if self.class.name != 'Stance::Event' Rails.logger.debug "Event: #{full_name}" self.class.callback_methods&.each { |method| send method } end record.save if @options[:record] end end self end |
#full_name ⇒ Object
70 71 72 |
# File 'lib/stance/event.rb', line 70 def full_name "#{subject.class.name.downcase}.#{name}" end |
#subject ⇒ Object
74 75 76 |
# File 'lib/stance/event.rb', line 74 def subject try(:record)&.subject || @subject end |