Class: Cellect::Server::GroupedWorkflow

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

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)



49
50
51
52
53
54
55
56
57
# File 'lib/cellect/server/grouped_workflow.rb', line 49

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 or samples one randomly with a fall back to a new group



16
17
18
# File 'lib/cellect/server/grouped_workflow.rb', line 16

def group(group_id = nil)
  subjects[group_id] || subjects.values.sample || fetch_or_setup_group(group_id)
end

#grouped?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/cellect/server/grouped_workflow.rb', line 84

def grouped?
  true
end

#groupsObject



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



66
67
68
69
70
# File 'lib/cellect/server/grouped_workflow.rb', line 66

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
}


33
34
35
36
37
38
39
# File 'lib/cellect/server/grouped_workflow.rb', line 33

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

#statusObject

General information about this workflow



73
74
75
76
77
78
79
80
81
82
# File 'lib/cellect/server/grouped_workflow.rb', line 73

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



21
22
23
# File 'lib/cellect/server/grouped_workflow.rb', line 21

def unseen_for(user_name, group_id: nil, limit: 5)
  group(group_id).subtract user(user_name).seen, limit
end