Class: CodeLabs::Tech

Inherits:
Object
  • Object
show all
Defined in:
lib/code_labs/tech.rb

Overview

require ‘pry’

Constant Summary collapse

@@all =

A peice of tech covered in the lab

Example:

CodeLabs::Tech.new(name) <- does not add to .all
CodeLabs::Tech.create(name) <- does add to .all
CodeLabs::Tech.find_or_create(name) <- will return the tech if it exists or will return a new one

Arguments:

title, duration, link, author, last_updated
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tech

All tech has a name, no?



16
17
18
19
# File 'lib/code_labs/tech.rb', line 16

def initialize(name) # All tech has a name, no?
    @name = name
    @labs = []
end

Instance Attribute Details

#nameObject (readonly)

<- The manager



14
15
16
# File 'lib/code_labs/tech.rb', line 14

def name
  @name
end

Class Method Details

.allObject



33
34
35
# File 'lib/code_labs/tech.rb', line 33

def self.all
    @@all # <- the core of the cli listing is in here
end

.clear_allObject



36
37
38
# File 'lib/code_labs/tech.rb', line 36

def self.clear_all
    @@all.clear # <- I do not think I ended up needing this
end

.create(name) ⇒ Object



39
40
41
# File 'lib/code_labs/tech.rb', line 39

def self.create(name)
    self.new(name).save # <- fancy
end

.find_or_create(name) ⇒ Object

<- the meat of the creation



42
43
44
45
# File 'lib/code_labs/tech.rb', line 42

def self.find_or_create(name) # <- the meat of the creation
    res = @@all.detect{|tech| tech.name == name}
    res ? res : self.create(name)
end

Instance Method Details

#add_lab(lab) ⇒ Object

<- type enforcment

Raises:

  • (TypeError)


27
28
29
30
31
# File 'lib/code_labs/tech.rb', line 27

def add_lab(lab) # <- type enforcment
    raise TypeError unless lab.is_a?(CodeLabs::Lab)
    @labs << lab
    lab.add_tech(self) unless lab.techs.include?(self) # <- infinite loop
end

#labsObject



24
25
26
# File 'lib/code_labs/tech.rb', line 24

def labs
    @labs.dup.freeze # <- no type editing here
end

#saveObject



20
21
22
23
# File 'lib/code_labs/tech.rb', line 20

def save
    @@all << self
    self # return the object not the list
end