Class: TrakFlow::Models::Comment
- Inherits:
-
Object
- Object
- TrakFlow::Models::Comment
- Defined in:
- lib/trak_flow/models/comment.rb
Overview
Represents a comment on a task
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#body ⇒ Object
Returns the value of attribute body.
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#id ⇒ Object
Returns the value of attribute id.
-
#task_id ⇒ Object
Returns the value of attribute task_id.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Class Method Summary collapse
Instance Method Summary collapse
- #errors ⇒ Object
-
#initialize(attrs = {}) ⇒ Comment
constructor
A new instance of Comment.
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #touch! ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
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] = 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
#author ⇒ Object
Returns the value of attribute author.
7 8 9 |
# File 'lib/trak_flow/models/comment.rb', line 7 def end |
#body ⇒ Object
Returns the value of attribute body.
7 8 9 |
# File 'lib/trak_flow/models/comment.rb', line 7 def body @body end |
#created_at ⇒ Object
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 |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/trak_flow/models/comment.rb', line 7 def id @id end |
#task_id ⇒ Object
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_at ⇒ Object
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
#errors ⇒ Object
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_h ⇒ Object
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: , 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
18 19 20 |
# File 'lib/trak_flow/models/comment.rb', line 18 def valid? errors.empty? end |
#validate! ⇒ Object
29 30 31 |
# File 'lib/trak_flow/models/comment.rb', line 29 def validate! raise ValidationError, errors.join(", ") unless valid? end |