Class: TrakFlow::Models::Label

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

Overview

Represents a label attached to a task Labels provide flexible, multi-dimensional categorization beyond structured fields like status, priority, and type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Label

Returns a new instance of Label.



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

def initialize(attrs = {})
  @id = attrs[:id] || SecureRandom.uuid
  @task_id = attrs[:task_id]
  @name = attrs[:name]
  @created_at = attrs[:created_at] || Time.now.utc
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



9
10
11
# File 'lib/trak_flow/models/label.rb', line 9

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/trak_flow/models/label.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/trak_flow/models/label.rb', line 9

def name
  @name
end

#task_idObject

Returns the value of attribute task_id.



9
10
11
# File 'lib/trak_flow/models/label.rb', line 9

def task_id
  @task_id
end

Class Method Details

.from_hash(hash) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/trak_flow/models/label.rb', line 65

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

.from_json(json_string) ⇒ Object



75
76
77
# File 'lib/trak_flow/models/label.rb', line 75

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

Instance Method Details

#dimensionObject

Labels can use dimension:value format for state caching



35
36
37
38
39
# File 'lib/trak_flow/models/label.rb', line 35

def dimension
  return nil unless name&.include?(":")

  name.split(":").first
end

#errorsObject



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

def errors
  errs = []
  errs << "Task ID is required" if task_id.nil? || task_id.to_s.strip.empty?
  errs << "Name is required" if name.nil? || name.strip.empty?
  errs << "Invalid label name" unless valid_name?
  errs
end

#state_label?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/trak_flow/models/label.rb', line 47

def state_label?
  name&.include?(":")
end

#to_hObject



51
52
53
54
55
56
57
58
# File 'lib/trak_flow/models/label.rb', line 51

def to_h
  {
    id: id,
    task_id: task_id,
    name: name,
    created_at: created_at&.iso8601
  }.compact
end

#to_json(*args) ⇒ Object



60
61
62
# File 'lib/trak_flow/models/label.rb', line 60

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end

#validate!Object

Raises:



30
31
32
# File 'lib/trak_flow/models/label.rb', line 30

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

#valueObject



41
42
43
44
45
# File 'lib/trak_flow/models/label.rb', line 41

def value
  return name unless name&.include?(":")

  name.split(":", 2).last
end