Class: SelfControl::Step
- Inherits:
-
Object
- Object
- SelfControl::Step
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/self-control/step.rb,
lib/self-control/step_adapter.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
Returns the value of attribute model.
Instance Method Summary collapse
- #actor ⇒ Object
- #actor_method ⇒ Object
- #allow?(person = nil) ⇒ Boolean
- #as_json(options = {}) ⇒ Object
- #condition?(attr) ⇒ Boolean
- #do!(*args) ⇒ Object
- #done? ⇒ Boolean
-
#initialize(builder, flow) ⇒ Step
constructor
A new instance of Step.
- #model_collection ⇒ Object
- #model_id ⇒ Object
- #model_name ⇒ Object
- #persisted? ⇒ Boolean
- #to_key ⇒ Object
Constructor Details
#initialize(builder, flow) ⇒ Step
Returns a new instance of Step.
7 8 9 10 11 |
# File 'lib/self-control/step.rb', line 7 def initialize(builder, flow) @builder = builder @flow = flow @model = flow.model end |
Instance Attribute Details
#model ⇒ Object
Returns the value of attribute model.
5 6 7 |
# File 'lib/self-control/step.rb', line 5 def model @model end |
Instance Method Details
#actor ⇒ Object
48 49 50 51 |
# File 'lib/self-control/step.rb', line 48 def actor return if actor_method.nil? @model.respond_to?(actor_method) ? @model.send(actor_method) : nil end |
#actor_method ⇒ Object
31 32 33 34 |
# File 'lib/self-control/step.rb', line 31 def actor_method return if @builder.actor.nil? @builder.actor.to_sym end |
#allow?(person = nil) ⇒ Boolean
53 54 55 |
# File 'lib/self-control/step.rb', line 53 def allow?(person=nil) actor ? person == actor : true end |
#as_json(options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/self-control/step_adapter.rb', line 16 def as_json(={}) { :id => to_param, :name => name, :actor => actor_method, :actions => actions, :default => default, :doable => doable?, :done => done?, :meta => } end |
#condition?(attr) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/self-control/step.rb', line 17 def condition?(attr) return attr if attr.is_a?(Boolean) result = if attr.is_a?(Proc) attr.call(*[@model,@builder].take(attr.arity >= 0 ? attr.arity : 0)) elsif @model.respond_to?(attr) @model.send(attr) else false end (result == 0 ? false : result.present?) end |
#do!(*args) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/self-control/step.rb', line 57 def do!(*args) = args. action = .delete(:choose) || args[0] || default return false unless action && actions.include?(action.to_sym) && @model.respond_to?(action) @model.attributes = [model_name] if [model_name] @model.send(action) @model.save end |
#done? ⇒ Boolean
13 14 15 |
# File 'lib/self-control/step.rb', line 13 def done? doable? && !condition?(@builder.if || false) && condition?(@builder.unless || true) end |
#model_collection ⇒ Object
44 45 46 |
# File 'lib/self-control/step.rb', line 44 def model_collection ActiveModel::Naming.plural(@model) if defined?(ActiveModel::Naming) end |
#model_id ⇒ Object
36 37 38 |
# File 'lib/self-control/step.rb', line 36 def model_id @model.id if @model.respond_to?(:id) end |
#model_name ⇒ Object
40 41 42 |
# File 'lib/self-control/step.rb', line 40 def model_name ActiveModel::Naming.singular(@model) if defined?(ActiveModel::Naming) end |
#persisted? ⇒ Boolean
8 9 10 |
# File 'lib/self-control/step_adapter.rb', line 8 def persisted? true end |
#to_key ⇒ Object
12 13 14 |
# File 'lib/self-control/step_adapter.rb', line 12 def to_key [model_name, model_id, name] end |