Class: StudentProgress::Unit

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

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Unit

Returns a new instance of Unit.



15
16
17
18
19
20
# File 'lib/student_progress/unit.rb', line 15

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

Instance Attribute Details

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#lessonsObject (readonly)

Returns the value of attribute lessons.



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

def lessons
  @lessons
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#topicObject

Returns the value of attribute topic.



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

def topic
  @topic
end

Class Method Details

.allObject



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

def self.all
  @@all
end

.create_from_hash(hash) ⇒ Object



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

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

Instance Method Details

#add_lesson(lesson) ⇒ Object



27
28
29
30
31
# File 'lib/student_progress/unit.rb', line 27

def add_lesson(lesson)
  @lessons << lesson unless @lessons.include?(lesson)
  lesson.unit = self unless lesson.unit == self
  lesson
end

#lessons_by_typeObject



38
39
40
# File 'lib/student_progress/unit.rb', line 38

def lessons_by_type
  @lessons.map{|l| "#{l.title} - #{l.content_type} (#{l.id})"}
end

#saveObject



22
23
24
25
# File 'lib/student_progress/unit.rb', line 22

def save 
  @@all << self 
  self
end

#summaryObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/student_progress/unit.rb', line 42

def summary
  { 
    topic: topic,
    unit: title,
    lessons: lessons_by_type,
    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