Class: Foobara::ErrorKey

Inherits:
Object
  • Object
show all
Defined in:
foobara-0.0.110/projects/common/src/error_key.rb

Overview

TODO: path’s have use outside of errors. Make this more general.

Constant Summary collapse

EMPTY_PATH =

TODO: use this wherever it makes sense

[].freeze
INDEX_VALUE =
/\A\d+\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol: nil, category: nil, path: EMPTY_PATH, runtime_path: EMPTY_PATH) ⇒ ErrorKey

TODO: accept error_class instead of symbol/category??



52
53
54
55
56
57
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 52

def initialize(symbol: nil, category: nil, path: EMPTY_PATH, runtime_path: EMPTY_PATH)
  self.category = symbolize(category)
  self.symbol = symbolize(symbol)
  self.path = symbolize(path)
  self.runtime_path = symbolize(runtime_path)
end

Instance Attribute Details

#categoryObject

Returns the value of attribute category.



49
50
51
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 49

def category
  @category
end

#pathObject

Returns the value of attribute path.



49
50
51
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 49

def path
  @path
end

#runtime_pathObject

Returns the value of attribute runtime_path.



49
50
51
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 49

def runtime_path
  @runtime_path
end

#symbolObject

Returns the value of attribute symbol.



49
50
51
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 49

def symbol
  @symbol
end

Class Method Details

.parse(key_string) ⇒ Object

key contains.…… a;b;c;d.e.f.g.h Then a, b, c is the runtime path and d is the category and e,f,g is the data path and h is the symbol



37
38
39
40
41
42
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 37

def parse(key_string)
  *runtime_path, key_string = key_string.to_s.split(">")
  category, *path, symbol = key_string.split(".")

  new(category:, path:, symbol:, runtime_path:)
end

.prepend_path(key) ⇒ Object



16
17
18
19
20
21
22
23
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 16

def prepend_path(key, *)
  if key.is_a?(ErrorKey)
    key.prepend_path(*).to_s
  else
    key = parse(key)
    key.prepend_path!(*).to_s
  end
end

.prepend_runtime_path(key, prepend_part) ⇒ Object



25
26
27
28
29
30
31
32
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 25

def prepend_runtime_path(key, prepend_part)
  if key.is_a?(ErrorKey)
    key.prepend_runtime_path(prepend_part).to_s
  else
    key = parse(key)
    key.prepend_runtime_path!(prepend_part).to_s
  end
end

.to_h(key_string) ⇒ Object



44
45
46
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 44

def to_h(key_string)
  parse(key_string).to_h
end

.to_s_type(key) ⇒ Object



8
9
10
11
12
13
14
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 8

def to_s_type(key)
  unless key.is_a?(ErrorKey)
    key = parse(key)
  end

  key.to_s_type
end

Instance Method Details

#prepend_pathObject



88
89
90
91
92
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 88

def prepend_path(*)
  dup.tap do |key|
    key.prepend_path!(*)
  end
end

#prepend_path!(*prepend_parts) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 75

def prepend_path!(*prepend_parts)
  if prepend_parts.size == 1
    arg = prepend_parts.first

    if arg.is_a?(Array)
      prepend_parts = arg
    end
  end

  self.path = [*prepend_parts, *path]
  self
end

#prepend_runtime_path(prepend_parts) ⇒ Object



99
100
101
102
103
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 99

def prepend_runtime_path(prepend_parts)
  dup.tap do |key|
    key.runtime_path = [*prepend_parts, *runtime_path]
  end
end

#prepend_runtime_path!(prepend_parts) ⇒ Object



94
95
96
97
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 94

def prepend_runtime_path!(prepend_parts)
  self.runtime_path = [*prepend_parts, *runtime_path]
  self
end

#to_hObject



130
131
132
133
134
135
136
137
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 130

def to_h
  {
    path:,
    runtime_path:,
    category:,
    symbol:
  }
end

#to_s(convert_to_type: false) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 107

def to_s(convert_to_type: false)
  path = self.path

  if convert_to_type
    path = path.map do |path_part|
      path_part =~ INDEX_VALUE ? :"#" : path_part
    end
  end

  [
    *runtime_path,
    [category, *path, symbol].join(".")
  ].join(">")
end

#to_s_typeObject



126
127
128
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 126

def to_s_type
  to_s(convert_to_type: true)
end

#to_symObject



122
123
124
# File 'foobara-0.0.110/projects/common/src/error_key.rb', line 122

def to_sym(**)
  to_s(**).to_sym
end