Class: SelfControl::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/self-control/flow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#builderObject (readonly)

Returns the value of attribute builder.



3
4
5
# File 'lib/self-control/flow.rb', line 3

def builder
  @builder
end

#modelObject (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

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/self-control/flow.rb', line 58

def allow?(actor=nil,options={})
  return false unless step = self[options[:to]]
  step.allow?(actor)
end

#completed_stepsObject



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)
  options = args.extract_options!
  return false unless step = self[args[0]]
  step.do!(args[1],options)
end

#done?Boolean

Returns:

  • (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(options={})
  count = (options[:of] || :all).to_sym == :doable ? total_doable : total 
  
  case (options[: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(options[:with] || ' / ')
  end
end

#stepsObject



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_completedObject



35
36
37
# File 'lib/self-control/flow.rb', line 35

def total_completed
  completed_steps.size
end