Class: Wicked::Pipeline::BaseStep
- Inherits:
-
Object
- Object
- Wicked::Pipeline::BaseStep
- Includes:
- ActiveModel::AttributeAssignment, ActiveModel::Attributes, ActiveModel::Validations
- Defined in:
- lib/wicked/pipeline/base_step.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#resource ⇒ ActiveRecord::Base
readonly
The record associated with the step.
Class Method Summary collapse
-
.==(other) ⇒ Boolean
Check whether two steps are the same.
-
.===(other) ⇒ Boolean
Check whether two steps are the same.
-
.eql?(other) ⇒ Boolean
Check whether two steps are the same.
-
.equal?(other) ⇒ Boolean
Check whether two steps are the same.
- .i18n_scope ⇒ Object
-
.step_name ⇒ String
Returns the step name of the step object.
-
.to_s ⇒ String
Returns the String representation of the step object.
Instance Method Summary collapse
-
#===(other) ⇒ Boolean
Check whether two steps are the same.
-
#blocking? ⇒ Boolean
Checks if the step is blocking subsequent steps in a pipeline.
-
#blocking_reason ⇒ String?
Returns the reason why a step is blocking, or
nil
if it is not blocking. -
#blocking_reasons ⇒ Hash{String,Symbol => Array<String>}
Returns reasons for each attribute why a step is blocking.
-
#equal?(other) ⇒ Boolean
Check whether two steps are the same.
-
#id ⇒ Number, Symbol
The ID of the resource or :new if the the resource is a new record.
-
#initialize(resource, params = nil) ⇒ BaseStep
constructor
A new instance of BaseStep.
-
#persisted? ⇒ Boolean
Checks if the resource exists in the database.
-
#readonly? ⇒ Boolean
Check if the step is readonly.
-
#save(**options, &block) ⇒ Boolean
Saves the resource.
-
#saved? ⇒ Boolean
Checks that the step has been successfully saved and there are no unsaved changes.
-
#step_name ⇒ String
Returns the step name of the step object.
-
#to_s ⇒ String
Returns the String representation of the step object.
-
#valid? ⇒ Boolean
Check if both the step and the resource are valid.
Constructor Details
#initialize(resource, params = nil) ⇒ BaseStep
Returns a new instance of BaseStep.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/wicked/pipeline/base_step.rb', line 17 def initialize(resource, params = nil) super() @resource = resource attributes.each_key do |key| public_send("#{key}=", resource.public_send(key)) if resource.respond_to?(key) end unless params.nil? parameters = _permit_params!(params) self.attributes = parameters.select { |key, _| respond_to?("#{key}=") } resource.attributes = parameters.select { |key, _| resource.respond_to?("#{key}=") } end regenerate_blocking_reasons! end |
Instance Attribute Details
#resource ⇒ ActiveRecord::Base (readonly)
Returns The record associated with the step.
13 14 15 |
# File 'lib/wicked/pipeline/base_step.rb', line 13 def resource @resource end |
Class Method Details
.==(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
191 192 193 |
# File 'lib/wicked/pipeline/base_step.rb', line 191 def self.==(other) super || to_s == other.to_s end |
.===(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
207 208 209 |
# File 'lib/wicked/pipeline/base_step.rb', line 207 def self.===(other) equal?(other) end |
.eql?(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
196 197 198 |
# File 'lib/wicked/pipeline/base_step.rb', line 196 def self.eql?(other) self == other end |
.equal?(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
202 203 204 |
# File 'lib/wicked/pipeline/base_step.rb', line 202 def self.equal?(other) self == other end |
.i18n_scope ⇒ Object
259 260 261 |
# File 'lib/wicked/pipeline/base_step.rb', line 259 def self.i18n_scope @_i18n_scope ||= name.underscore.tr("/", ".").prepend("steps.") end |
.step_name ⇒ String
Returns the step name of the step object
148 149 150 |
# File 'lib/wicked/pipeline/base_step.rb', line 148 def self.step_name @_step_name ||= to_s.gsub(%r{(?:\w+/)*}, "") end |
.to_s ⇒ String
Returns the String representation of the step object
135 136 137 |
# File 'lib/wicked/pipeline/base_step.rb', line 135 def self.to_s @_to_s ||= name.to_s.underscore.sub(/_step\z/, "") end |
Instance Method Details
#===(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
217 218 219 |
# File 'lib/wicked/pipeline/base_step.rb', line 217 def ===(other) equal?(other) end |
#blocking? ⇒ Boolean
Checks if the step is blocking subsequent steps in a pipeline. In other words, if a step is “blocking”, all the subsequent steps in the pipeline will be marked as inaccessible.
This can be used to short-circuit a pipeline.
89 90 91 |
# File 'lib/wicked/pipeline/base_step.rb', line 89 def blocking? false end |
#blocking_reason ⇒ String?
Returns the reason why a step is blocking, or nil
if it is not blocking.
This should be used to set an overall blocking reason. To set per-attribute blocking reasons use #blocking_reasons.
108 109 110 |
# File 'lib/wicked/pipeline/base_step.rb', line 108 def blocking_reason nil end |
#blocking_reasons ⇒ Hash{String,Symbol => Array<String>}
This method should not be overridden, it should just be used to access blocking reasons. To add a new reason, just add it to the hash. To reset all the reasons, override the #regenerate_blocking_reasons! method in subsclasses.
Warning: Every time #valid? is called the blocking reasons will be regenereted.
Returns reasons for each attribute why a step is blocking.
122 123 124 |
# File 'lib/wicked/pipeline/base_step.rb', line 122 def blocking_reasons @blocking_reasons ||= ActiveSupport::HashWithIndifferentAccess.new { |h, k| h[k] = [] } end |
#equal?(other) ⇒ Boolean
Check whether two steps are the same
It returns true
only if other
is the same step class then self
or if self.to_s
is equal to other
. other.step_name
.
212 213 214 |
# File 'lib/wicked/pipeline/base_step.rb', line 212 def equal?(other) self.class.equal?(other) end |
#id ⇒ Number, Symbol
Returns The ID of the resource or :new if the the resource is a new record.
222 223 224 |
# File 'lib/wicked/pipeline/base_step.rb', line 222 def id resource.id || :new end |
#persisted? ⇒ Boolean
Checks if the resource exists in the database
Always returns true
if the step is readonly
69 70 71 |
# File 'lib/wicked/pipeline/base_step.rb', line 69 def persisted? readonly? || resource.persisted? end |
#readonly? ⇒ Boolean
Check if the step is readonly
96 97 98 |
# File 'lib/wicked/pipeline/base_step.rb', line 96 def readonly? false end |
#save(**options, &block) ⇒ Boolean
Saves the resource
If the step is readonly the record will not be saved and the method will return true
41 42 43 44 45 46 47 |
# File 'lib/wicked/pipeline/base_step.rb', line 41 def save(**, &block) return true if readonly? cleanup_stale_data valid? && resource.save(**, &block) end |
#saved? ⇒ Boolean
Checks that the step has been successfully saved and there are no unsaved changes
Always returns true
if the step is readonly
78 79 80 |
# File 'lib/wicked/pipeline/base_step.rb', line 78 def saved? readonly? || (persisted? && resource.saved_changes? && !resource.has_changes_to_save?) end |
#step_name ⇒ String
Returns the step name of the step object
178 179 180 |
# File 'lib/wicked/pipeline/base_step.rb', line 178 def step_name self.class.step_name end |
#to_s ⇒ String
Returns the String representation of the step object
163 164 165 |
# File 'lib/wicked/pipeline/base_step.rb', line 163 def to_s self.class.to_s end |
#valid? ⇒ Boolean
Check if both the step and the resource are valid
Always returns true
if the step is readonly
54 55 56 57 58 59 60 61 62 |
# File 'lib/wicked/pipeline/base_step.rb', line 54 def valid? return true if readonly? is_valid = super resource.validate resource.errors.merge!(errors) regenerate_blocking_reasons! is_valid && resource.errors.none? end |