Class: Jsm::ClientExtension
- Inherits:
-
Object
- Object
- Jsm::ClientExtension
- Defined in:
- lib/jsm/client_extension.rb
Overview
Jsm::ClientExtension define all custom methods for the model that use the state machine class E.g: Define method for states
Instance Attribute Summary collapse
-
#event_executor ⇒ Object
readonly
Returns the value of attribute event_executor.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#state_machine ⇒ Object
readonly
Returns the value of attribute state_machine.
Class Method Summary collapse
Instance Method Summary collapse
-
#define_event_method ⇒ Object
define all event that has been defined it consists of 3 types of method: * method to check whether can do event( e.g: can_married? ) * method to run an event and return bollean( e.g: married ) * method to run an event and raise error if failed( e.g: married! ).
-
#define_states_method ⇒ Object
define method for all states to check status equal with a states e.g: states: [:x, :y] it will define method ‘x?` and `y?`.
-
#initialize(klass, params = {}) ⇒ ClientExtension
constructor
A new instance of ClientExtension.
Constructor Details
#initialize(klass, params = {}) ⇒ ClientExtension
Returns a new instance of ClientExtension.
11 12 13 14 15 16 17 18 |
# File 'lib/jsm/client_extension.rb', line 11 def initialize(klass, params = {}) @klass = klass @state_machine = params[:state_machine] @event_executor = klass.jsm_event_executor.new(validators: @state_machine.validators) unless @state_machine.attribute_name raise Jsm::NoAttributeError, "please assign the attribute_name first in class #{@state_machine.name}" end end |
Instance Attribute Details
#event_executor ⇒ Object (readonly)
Returns the value of attribute event_executor.
10 11 12 |
# File 'lib/jsm/client_extension.rb', line 10 def event_executor @event_executor end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'lib/jsm/client_extension.rb', line 10 def klass @klass end |
#state_machine ⇒ Object (readonly)
Returns the value of attribute state_machine.
10 11 12 |
# File 'lib/jsm/client_extension.rb', line 10 def state_machine @state_machine end |
Class Method Details
.decorate(klass, params = {}) ⇒ Object
4 5 6 7 8 |
# File 'lib/jsm/client_extension.rb', line 4 def self.decorate(klass, params = {}) client_extension = new(klass, params) client_extension.define_states_method client_extension.define_event_method end |
Instance Method Details
#define_event_method ⇒ Object
define all event that has been defined it consists of 3 types of method:
-
method to check whether can do event( e.g: can_married? )
-
method to run an event and return bollean( e.g: married )
-
method to run an event and raise error if failed( e.g: married! )
37 38 39 40 41 42 43 44 |
# File 'lib/jsm/client_extension.rb', line 37 def define_event_method state_machine.events.each do |event_name, event| event.attribute_name = state_machine.attribute_name define_can_event_method(event_name, event) define_event_execution_method(event_name, event) define_event_execution_method!(event_name, event) end end |
#define_states_method ⇒ Object
define method for all states to check status equal with a states e.g: states: [:x, :y] it will define method ‘x?` and `y?`
23 24 25 26 27 28 29 30 |
# File 'lib/jsm/client_extension.rb', line 23 def define_states_method state_machine.states.each do |state| state_name = state.name klass.send(:define_method, "#{state_name}?".to_sym) do self.current_state == state_name end end end |