Class: Tutorial::Task

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/nexmo_developer/app/models/tutorial/task.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, current_step:, code_language: nil, title: nil, description: nil) ⇒ Task

Returns a new instance of Task.



8
9
10
11
12
13
14
15
16
17
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 8

def initialize(name:, current_step:, code_language: nil, title: nil, description: nil)
  @name         = name
  @title        = title
  @description  = description
  @code_language = code_language
  @current_step = current_step
  @file_loader = load_file!

  @errors = ActiveModel::Errors.new(self)
end

Instance Attribute Details

#code_languageObject (readonly)

Returns the value of attribute code_language.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 4

def code_language
  @code_language
end

#current_stepObject (readonly)

Returns the value of attribute current_step.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 4

def current_step
  @current_step
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 4

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 4

def name
  @name
end

Class Method Details

.human_attribute_name(attr, _options = {}) ⇒ Object



77
78
79
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 77

def self.human_attribute_name(attr, _options = {})
  attr
end

.lookup_ancestorsObject



81
82
83
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 81

def self.lookup_ancestors
  [self]
end

.make_from(name:, code_language:, current_step:) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 40

def self.make_from(name:, code_language:, current_step:)
  new(
    name: name,
    code_language: code_language,
    current_step: current_step
  )
end

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
53
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 48

def ==(other)
  name == other.name &&
    title == other.title &&
    description == other.description &&
    current_step == other.current_step
end

#active?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 36

def active?
  @name == @current_step
end

#descriptionObject



67
68
69
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 67

def description
  @description || yaml['description']
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 55

def eql?(other)
  self == other
end

#hashObject



59
60
61
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 59

def hash
  name.hash ^ title.hash ^ description.hash ^ current_step.hash
end

#load_file!Object



19
20
21
22
23
24
25
26
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 19

def load_file!
  Tutorial::FileLoader.new(
    root: Tutorial.task_content_path,
    doc_name: @name,
    code_language: @code_language,
    format: 'md'
  )
end

#read_attribute_for_validation(attr) ⇒ Object

The following methods are needed for validation



73
74
75
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 73

def read_attribute_for_validation(attr)
  send(attr)
end

#titleObject



63
64
65
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 63

def title
  @title || yaml['title']
end

#validate!Object



28
29
30
31
32
33
34
# File 'lib/nexmo_developer/app/models/tutorial/task.rb', line 28

def validate!
  unless ['introduction', 'conclusion', 'prerequisites'].include? name
    path.present?
  end
rescue ::Nexmo::Markdown::DocFinder::MissingDoc => _e
  @errors.add(:name, message: "could not find the file: #{name}")
end