Class: SelfControl::Step

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/self-control/step.rb', line 5

def model
  @model
end

Instance Method Details

#actorObject



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_methodObject



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

Returns:

  • (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(options={})
  {
    :id => to_param,
    :name => name,
    :actor => actor_method,
    :actions => actions,
    :default => default,
    :doable => doable?,
    :done => done?,
    :meta => meta
  }
end

#condition?(attr) ⇒ Boolean

Returns:

  • (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)
  options = args.extract_options!
  action = options.delete(:choose) || args[0] || default
  return false unless action && actions.include?(action.to_sym) && @model.respond_to?(action)
  
  @model.attributes = options[model_name] if options[model_name]
  @model.send(action)
  @model.save
end

#done?Boolean

Returns:

  • (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_collectionObject



44
45
46
# File 'lib/self-control/step.rb', line 44

def model_collection
  ActiveModel::Naming.plural(@model) if defined?(ActiveModel::Naming)
end

#model_idObject



36
37
38
# File 'lib/self-control/step.rb', line 36

def model_id
  @model.id if @model.respond_to?(:id)
end

#model_nameObject



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

Returns:

  • (Boolean)


8
9
10
# File 'lib/self-control/step_adapter.rb', line 8

def persisted?
  true
end

#to_keyObject



12
13
14
# File 'lib/self-control/step_adapter.rb', line 12

def to_key
  [model_name, model_id, name]
end