Class: TrakFlow::Models::Comment

Inherits:
Object
  • Object
show all
Defined in:
lib/trak_flow/models/comment.rb

Overview

Represents a comment on a task

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Comment

Returns a new instance of Comment.



9
10
11
12
13
14
15
16
# File 'lib/trak_flow/models/comment.rb', line 9

def initialize(attrs = {})
  @id = attrs[:id] || SecureRandom.uuid
  @task_id = attrs[:task_id]
  @author = attrs[:author] || TrakFlow.config.get("actor")
  @body = attrs[:body]
  @created_at = attrs[:created_at] || Time.now.utc
  @updated_at = attrs[:updated_at] || Time.now.utc
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def author
  @author
end

#bodyObject

Returns the value of attribute body.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def body
  @body
end

#created_atObject

Returns the value of attribute created_at.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def id
  @id
end

#task_idObject

Returns the value of attribute task_id.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def task_id
  @task_id
end

#updated_atObject

Returns the value of attribute updated_at.



7
8
9
# File 'lib/trak_flow/models/comment.rb', line 7

def updated_at
  @updated_at
end

Class Method Details

.from_hash(hash) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/trak_flow/models/comment.rb', line 53

def from_hash(hash)
  hash = hash.transform_keys(&:to_sym)
  new(
    id: hash[:id],
    task_id: hash[:task_id],
    author: hash[:author],
    body: hash[:body],
    created_at: TimeParser.parse(hash[:created_at]),
    updated_at: TimeParser.parse(hash[:updated_at])
  )
end

.from_json(json_string) ⇒ Object



65
66
67
# File 'lib/trak_flow/models/comment.rb', line 65

def from_json(json_string)
  from_hash(Oj.load(json_string, mode: :compat, symbol_keys: true))
end

Instance Method Details

#errorsObject



22
23
24
25
26
27
# File 'lib/trak_flow/models/comment.rb', line 22

def errors
  errs = []
  errs << "Task ID is required" if task_id.nil? || task_id.to_s.strip.empty?
  errs << "Body is required" if body.nil? || body.strip.empty?
  errs
end

#to_hObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/trak_flow/models/comment.rb', line 37

def to_h
  {
    id: id,
    task_id: task_id,
    author: author,
    body: body,
    created_at: created_at&.iso8601,
    updated_at: updated_at&.iso8601
  }.compact
end

#to_json(*args) ⇒ Object



48
49
50
# File 'lib/trak_flow/models/comment.rb', line 48

def to_json(*args)
  Oj.dump(to_h, mode: :compat)
end

#touch!Object



33
34
35
# File 'lib/trak_flow/models/comment.rb', line 33

def touch!
  self.updated_at = Time.now.utc
end

#valid?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/trak_flow/models/comment.rb', line 18

def valid?
  errors.empty?
end

#validate!Object

Raises:



29
30
31
# File 'lib/trak_flow/models/comment.rb', line 29

def validate!
  raise ValidationError, errors.join(", ") unless valid?
end