Class: Cellect::Server::Workflow
- Inherits:
-
Object
- Object
- Cellect::Server::Workflow
- Includes:
- Celluloid
- Defined in:
- lib/cellect/server/workflow.rb
Direct Known Subclasses
Constant Summary collapse
- SET_KLASS =
{ # priority, pairwise [ false, false ] => DiffSet::RandomSet, [ false, true ] => DiffSet::PairwiseRandomSet, [ true, false ] => DiffSet::PrioritySet, [ true, true ] => DiffSet::PairwisePrioritySet }
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#pairwise ⇒ Object
Returns the value of attribute pairwise.
-
#prioritized ⇒ Object
Returns the value of attribute prioritized.
-
#state ⇒ Object
Returns the value of attribute state.
-
#subjects ⇒ Object
Returns the value of attribute subjects.
-
#users ⇒ Object
Returns the value of attribute users.
Class Method Summary collapse
Instance Method Summary collapse
- #add(opts = { }) ⇒ Object
- #add_seen_for(user_id, *subject_ids) ⇒ Object
- #grouped? ⇒ Boolean
-
#initialize(name, pairwise: false, prioritized: false) ⇒ Workflow
constructor
A new instance of Workflow.
- #load_data ⇒ Object
- #pairwise? ⇒ Boolean
- #prioritized? ⇒ Boolean
- #ready? ⇒ Boolean
- #remove(opts = { }) ⇒ Object
- #remove_user(user_id) ⇒ Object
- #sample(opts = { }) ⇒ Object
- #set_klass ⇒ Object
- #status ⇒ Object
- #unseen_for(user_id, limit: 5) ⇒ Object
- #user(id) ⇒ Object
Constructor Details
#initialize(name, pairwise: false, prioritized: false) ⇒ Workflow
Returns a new instance of Workflow.
25 26 27 28 29 30 31 32 |
# File 'lib/cellect/server/workflow.rb', line 25 def initialize(name, pairwise: false, prioritized: false) self.name = name self.users = { } self.pairwise = !!pairwise self.prioritized = !!prioritized self.subjects = set_klass.new load_data end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def name @name end |
#pairwise ⇒ Object
Returns the value of attribute pairwise.
7 8 9 |
# File 'lib/cellect/server/workflow.rb', line 7 def pairwise @pairwise end |
#prioritized ⇒ Object
Returns the value of attribute prioritized.
7 8 9 |
# File 'lib/cellect/server/workflow.rb', line 7 def prioritized @prioritized end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def state @state end |
#subjects ⇒ Object
Returns the value of attribute subjects.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def subjects @subjects end |
#users ⇒ Object
Returns the value of attribute users.
6 7 8 |
# File 'lib/cellect/server/workflow.rb', line 6 def users @users end |
Class Method Details
.[](name, pairwise: false, prioritized: false) ⇒ Object
9 10 11 12 13 |
# File 'lib/cellect/server/workflow.rb', line 9 def self.[](name, pairwise: false, prioritized: false) key = "workflow_#{ name }".to_sym Actor[key] ||= supervise name, pairwise: pairwise, prioritized: prioritized Actor[key].actors.first end |
.all ⇒ Object
21 22 23 |
# File 'lib/cellect/server/workflow.rb', line 21 def self.all names.collect{ |name| Workflow[name] } end |
.names ⇒ Object
15 16 17 18 19 |
# File 'lib/cellect/server/workflow.rb', line 15 def self.names actor_names = Celluloid.actor_system.registry.names.collect &:to_s workflow_actors = actor_names.select{ |key| key =~ /^workflow_/ } workflow_actors.collect{ |name| name.sub(/^workflow_/, '').to_sym } end |
Instance Method Details
#add(opts = { }) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/cellect/server/workflow.rb', line 71 def add(opts = { }) if prioritized? subjects.add opts[:subject_id], opts[:priority] else subjects.add opts[:subject_id] end end |
#add_seen_for(user_id, *subject_ids) ⇒ Object
52 53 54 55 56 |
# File 'lib/cellect/server/workflow.rb', line 52 def add_seen_for(user_id, *subject_ids) [subject_ids].flatten.compact.each do |subject_id| user(user_id).seen.add subject_id end end |
#grouped? ⇒ Boolean
91 92 93 |
# File 'lib/cellect/server/workflow.rb', line 91 def grouped? false end |
#load_data ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/cellect/server/workflow.rb', line 34 def load_data self.state = :initializing self.subjects = set_klass.new Cellect::Server.adapter.load_data_for(name).each do |hash| subjects.add hash['id'], hash['priority'] end self.state = :ready end |
#pairwise? ⇒ Boolean
83 84 85 |
# File 'lib/cellect/server/workflow.rb', line 83 def pairwise? !!pairwise end |
#prioritized? ⇒ Boolean
87 88 89 |
# File 'lib/cellect/server/workflow.rb', line 87 def prioritized? !!prioritized end |
#ready? ⇒ Boolean
95 96 97 |
# File 'lib/cellect/server/workflow.rb', line 95 def ready? state == :ready end |
#remove(opts = { }) ⇒ Object
79 80 81 |
# File 'lib/cellect/server/workflow.rb', line 79 def remove(opts = { }) subjects.remove opts[:subject_id] end |
#remove_user(user_id) ⇒ Object
58 59 60 61 |
# File 'lib/cellect/server/workflow.rb', line 58 def remove_user(user_id) removed = self.users.delete user_id removed.terminate if removed end |
#sample(opts = { }) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/cellect/server/workflow.rb', line 63 def sample(opts = { }) if opts[:user_id] unseen_for opts[:user_id], limit: opts[:limit] else subjects.sample opts[:limit] end end |
#set_klass ⇒ Object
107 108 109 |
# File 'lib/cellect/server/workflow.rb', line 107 def set_klass SET_KLASS[[prioritized, pairwise]] end |
#status ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cellect/server/workflow.rb', line 111 def status { state: state, grouped: false, prioritized: prioritized, pairwise: pairwise, subjects: subjects.size, users: users.length } end |
#unseen_for(user_id, limit: 5) ⇒ Object
48 49 50 |
# File 'lib/cellect/server/workflow.rb', line 48 def unseen_for(user_id, limit: 5) subjects.subtract user(user_id).seen, limit end |
#user(id) ⇒ Object
43 44 45 46 |
# File 'lib/cellect/server/workflow.rb', line 43 def user(id) self.users[id] ||= User.supervise id, workflow_name: name users[id].actors.first end |