Class: StudentProgress::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/student_progress/topic.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Topic

Returns a new instance of Topic.



19
20
21
22
23
24
# File 'lib/student_progress/topic.rb', line 19

def initialize(attrs = {})
  attrs.each do |attr, value|
    self.send("#{attr}=", value)
  end
  @units = []
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



2
3
4
# File 'lib/student_progress/topic.rb', line 2

def id
  @id
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/student_progress/topic.rb', line 2

def title
  @title
end

#unitsObject (readonly)

Returns the value of attribute units.



3
4
5
# File 'lib/student_progress/topic.rb', line 3

def units
  @units
end

Class Method Details

.allObject



7
8
9
# File 'lib/student_progress/topic.rb', line 7

def self.all 
  @@all
end

.create_from_hash(hash) ⇒ Object



11
12
13
# File 'lib/student_progress/topic.rb', line 11

def self.create_from_hash(hash)
  new(hash).save
end

.find_by_title(title) ⇒ Object



15
16
17
# File 'lib/student_progress/topic.rb', line 15

def self.find_by_title(title)
  all.find{|t| t.title == title}
end

Instance Method Details

#add_unit(unit) ⇒ Object



31
32
33
34
35
# File 'lib/student_progress/topic.rb', line 31

def add_unit(unit)
  @units << unit unless @units.include?(unit)
  unit.topic = self unless unit.topic == self
  unit
end

#lessonsObject



37
38
39
# File 'lib/student_progress/topic.rb', line 37

def lessons
  @units.map{|u| u.lessons}.flatten
end

#lessons_by_unitObject



41
42
43
44
45
46
47
# File 'lib/student_progress/topic.rb', line 41

def lessons_by_unit
  result = {}
  @units.each do |unit|
    result[unit.title] = unit.lessons.map{|l| "#{l.title} - #{l.content_type} (#{l.id})"}
  end
  result
end

#saveObject



26
27
28
29
# File 'lib/student_progress/topic.rb', line 26

def save 
  @@all << self 
  self
end

#summaryObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/student_progress/topic.rb', line 49

def summary
  {
    topic: title,
    units: @units.map{|u| u.title},
    lessons: lessons_by_unit,
    readmes: lessons.count{|l| l.content_type == "Readme"},
    labs: lessons.count{|l| l.content_type == "Lab"},
    projects: lessons.count{|l| l.content_type == "Project"}
  }
end