Class: Cellect::Server::GroupedWorkflow

Inherits:
Workflow
  • Object
show all
Defined in:
lib/cellect/server/grouped_workflow.rb

Constant Summary

Constants inherited from Workflow

Workflow::SET_KLASS

Instance Attribute Summary collapse

Attributes inherited from Workflow

#name, #pairwise, #prioritized, #state, #subjects, #users

Instance Method Summary collapse

Methods inherited from Workflow

[], []=, #add_seen_for, all, names, #pairwise?, #prioritized?, #ready?, #remove_user, #set_klass, #user

Constructor Details

#initialize(name, pairwise: false, prioritized: false) ⇒ GroupedWorkflow

Sets up the new workflow



7
8
9
10
# File 'lib/cellect/server/grouped_workflow.rb', line 7

def initialize(name, pairwise: false, prioritized: false)
  self.groups = { }
  super
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



4
5
6
# File 'lib/cellect/server/grouped_workflow.rb', line 4

def groups
  @groups
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)



58
59
60
61
62
63
64
# File 'lib/cellect/server/grouped_workflow.rb', line 58

def add(opts = { })
  if prioritized?
    groups[opts[:group_id]].add opts[:subject_id], opts[:priority]
  else
    groups[opts[:group_id]].add opts[:subject_id]
  end
end

#group(group_id = nil) ⇒ Object

Returns a group by id or samples one randomly



25
26
27
# File 'lib/cellect/server/grouped_workflow.rb', line 25

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

#grouped?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/cellect/server/grouped_workflow.rb', line 89

def grouped?
  true
end

#load_dataObject

Load subjects from the adapter into their groups



13
14
15
16
17
18
19
20
21
22
# File 'lib/cellect/server/grouped_workflow.rb', line 13

def load_data
  self.state = :initializing
  self.groups = { }
  klass = set_klass
  Cellect::Server.adapter.load_data_for(name).each do |hash|
    self.groups[hash['group_id']] ||= klass.new
    self.groups[hash['group_id']].add hash['id'], hash['priority']
  end
  self.state = :ready
end

#remove(opts = { }) ⇒ Object

Removes a subject from a group

Accepts a hash in the form:

group_id: 1,
subject_id: 2



73
74
75
# File 'lib/cellect/server/grouped_workflow.rb', line 73

def remove(opts = { })
  groups[opts[:group_id]].remove opts[:subject_id]
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
}


42
43
44
45
46
47
48
# File 'lib/cellect/server/grouped_workflow.rb', line 42

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



78
79
80
81
82
83
84
85
86
87
# File 'lib/cellect/server/grouped_workflow.rb', line 78

def status
  # Get the number of subjects in each group
  group_counts = Hash[*groups.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



30
31
32
# File 'lib/cellect/server/grouped_workflow.rb', line 30

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