Class: Cellect::Server::GroupedWorkflow
- Defined in:
- lib/cellect/server/grouped_workflow.rb
Constant Summary
Constants inherited from Workflow
Workflow::RELOAD_TIMEOUT, Workflow::SET_KLASS, Workflow::SKIP_RELOAD_STATES
Instance Attribute Summary
Attributes inherited from Workflow
#can_reload_at, #name, #pairwise, #prioritized, #state, #subjects, #users
Instance Method Summary collapse
-
#add(opts = { }) ⇒ Object
Adds or updates a subject in a group.
-
#group(group_id = nil) ⇒ Object
Returns a group by id if the group_id is supplied it will select or setup this group when no group_id is supplied, it attempts to select a random group finally if no loaded groupes it falls back to creating a new empty group.
- #grouped? ⇒ Boolean
- #groups ⇒ Object
-
#initialize(name, pairwise: false, prioritized: false) ⇒ GroupedWorkflow
constructor
Sets up the new workflow.
-
#remove(opts = { }) ⇒ Object
Removes a subject from a group.
-
#sample(opts = { }) ⇒ Object
Get a sample of subjects from a group for a user.
-
#status ⇒ Object
General information about this workflow.
-
#unseen_for(user_name, group_id: nil, limit: 5) ⇒ Object
Get unseen subjects from a group for a user.
Methods inherited from Workflow
[], []=, #add_seen_for, all, #can_reload_data?, #load_data, names, #pairwise?, #prioritized?, #ready?, #reload_data, #remove_user, #set_klass, #set_reload_at_time, #user
Constructor Details
#initialize(name, pairwise: false, prioritized: false) ⇒ GroupedWorkflow
Sets up the new workflow
6 7 8 9 |
# File 'lib/cellect/server/grouped_workflow.rb', line 6 def initialize(name, pairwise: false, prioritized: false) self.subjects = { } super end |
Instance Method Details
#add(opts = { }) ⇒ Object
Adds or updates a subject in a group
Accepts a hash in the form:
subject_id: 1,
group_id: 2,
priority: 0.5 # (if the workflow is prioritized)
57 58 59 60 61 62 63 64 65 |
# File 'lib/cellect/server/grouped_workflow.rb', line 57 def add(opts = { }) add_group = fetch_or_setup_group(opts[:group_id]) if prioritized? add_group.add opts[:subject_id], opts[:priority] else add_group.add opts[:subject_id] end end |
#group(group_id = nil) ⇒ Object
Returns a group by id if the group_id is supplied it will select or setup this group when no group_id is supplied, it attempts to select a random group finally if no loaded groupes it falls back to creating a new empty group
19 20 21 22 23 24 25 26 |
# File 'lib/cellect/server/grouped_workflow.rb', line 19 def group(group_id = nil) group = if group_id fetch_or_setup_group(group_id) else subjects.values.sample end group || fetch_or_setup_group(group_id) end |
#grouped? ⇒ Boolean
92 93 94 |
# File 'lib/cellect/server/grouped_workflow.rb', line 92 def grouped? true end |
#groups ⇒ Object
11 12 13 |
# File 'lib/cellect/server/grouped_workflow.rb', line 11 def groups subjects end |
#remove(opts = { }) ⇒ Object
Removes a subject from a group
Accepts a hash in the form:
group_id: 1,
subject_id: 2
74 75 76 77 78 |
# File 'lib/cellect/server/grouped_workflow.rb', line 74 def remove(opts = { }) if group = subjects[opts[:group_id]] group.remove opts[:subject_id] end end |
#sample(opts = { }) ⇒ Object
Get a sample of subjects from a group for a user
Accepts a hash in the form:
{
user_id: 123,
group_id: 2,
limit: 5
}
41 42 43 44 45 46 47 |
# File 'lib/cellect/server/grouped_workflow.rb', line 41 def sample(opts = { }) if opts[:user_id] unseen_for opts[:user_id], group_id: opts[:group_id], limit: opts[:limit] else group(opts[:group_id]).sample opts[:limit] end end |
#status ⇒ Object
General information about this workflow
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/cellect/server/grouped_workflow.rb', line 81 def status # Get the number of subjects in each group group_counts = Hash[*subjects.collect{ |id, set| [id, set.size] }.flatten] super.merge({ grouped: true, subjects: group_counts.values.inject(:+), groups: group_counts }) end |
#unseen_for(user_name, group_id: nil, limit: 5) ⇒ Object
Get unseen subjects from a group for a user
29 30 31 |
# File 'lib/cellect/server/grouped_workflow.rb', line 29 def unseen_for(user_name, group_id: nil, limit: 5) group(group_id).subtract user(user_name).seen, limit end |