Class: SelfControl::Flow
- Inherits:
-
Object
- Object
- SelfControl::Flow
- Defined in:
- lib/self-control/flow.rb
Instance Attribute Summary collapse
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #allow?(actor = nil, options = {}) ⇒ Boolean
- #completed_steps ⇒ Object
- #do!(*args) ⇒ Object
- #done? ⇒ Boolean
-
#initialize(builder, model) ⇒ Flow
constructor
A new instance of Flow.
- #next_step_for(actor) ⇒ Object
- #progress(options = {}) ⇒ Object
- #steps ⇒ Object
- #steps_for(actor) ⇒ Object
- #total_completed ⇒ Object
Constructor Details
#initialize(builder, model) ⇒ Flow
Returns a new instance of Flow.
7 8 9 10 11 |
# File 'lib/self-control/flow.rb', line 7 def initialize(builder, model) @builder = builder @model = model @step_map = {} end |
Instance Attribute Details
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
3 4 5 |
# File 'lib/self-control/flow.rb', line 3 def builder @builder end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/self-control/flow.rb', line 3 def model @model end |
Instance Method Details
#[](name) ⇒ Object
13 14 15 16 17 |
# File 'lib/self-control/flow.rb', line 13 def [](name) return if name.nil? name = name.to_sym @step_map[name] ||= steps.detect { |step| step.name == name } end |
#allow?(actor = nil, options = {}) ⇒ Boolean
58 59 60 61 |
# File 'lib/self-control/flow.rb', line 58 def allow?(actor=nil,={}) return false unless step = self[[:to]] step.allow?(actor) end |
#completed_steps ⇒ Object
31 32 33 |
# File 'lib/self-control/flow.rb', line 31 def completed_steps steps.select(&:done?) end |
#do!(*args) ⇒ Object
63 64 65 66 67 |
# File 'lib/self-control/flow.rb', line 63 def do!(*args) = args. return false unless step = self[args[0]] step.do!(args[1],) end |
#done? ⇒ Boolean
54 55 56 |
# File 'lib/self-control/flow.rb', line 54 def done? total_completed == total_doable end |
#next_step_for(actor) ⇒ Object
27 28 29 |
# File 'lib/self-control/flow.rb', line 27 def next_step_for(actor) steps.detect { |step| step.doable? && !step.done? && step.allow?(actor) } end |
#progress(options = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/self-control/flow.rb', line 39 def progress(={}) count = ([:of] || :all).to_sym == :doable ? total_doable : total case ([:as] || :percent).to_sym when :percent count == 0 ? 100 : (total_completed / count.to_f) * 100 when :decimal count == 0 ? 1.0 : (total_completed / count) when :array [total_completed, count] when :string [total_completed, count].join([:with] || ' / ') end end |
#steps ⇒ Object
19 20 21 |
# File 'lib/self-control/flow.rb', line 19 def steps @steps ||= @builder.steps.map { |step| SelfControl::Step.new(step, self) } end |
#steps_for(actor) ⇒ Object
23 24 25 |
# File 'lib/self-control/flow.rb', line 23 def steps_for(actor) steps.select { |step| step.allow?(actor) } end |
#total_completed ⇒ Object
35 36 37 |
# File 'lib/self-control/flow.rb', line 35 def total_completed completed_steps.size end |