Class: Steep::Services::PathAssignment

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/services/path_assignment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index:, max_index:) ⇒ PathAssignment

Returns a new instance of PathAssignment.



6
7
8
9
10
# File 'lib/steep/services/path_assignment.rb', line 6

def initialize(index:, max_index:)
  @index = index
  @max_index = max_index
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



4
5
6
# File 'lib/steep/services/path_assignment.rb', line 4

def cache
  @cache
end

#indexObject (readonly)

Returns the value of attribute index.



4
5
6
# File 'lib/steep/services/path_assignment.rb', line 4

def index
  @index
end

#max_indexObject (readonly)

Returns the value of attribute max_index.



4
5
6
# File 'lib/steep/services/path_assignment.rb', line 4

def max_index
  @max_index
end

Class Method Details

.allObject



12
13
14
# File 'lib/steep/services/path_assignment.rb', line 12

def self.all
  new(index: 0, max_index: 1)
end

.index_for(key:, max_index:) ⇒ Object



41
42
43
# File 'lib/steep/services/path_assignment.rb', line 41

def self.index_for(key:, max_index:)
  Digest::MD5.hexdigest(key).hex % max_index
end

Instance Method Details

#=~(target_path) ⇒ Object Also known as: ===



16
17
18
19
# File 'lib/steep/services/path_assignment.rb', line 16

def =~(target_path)
  key = stringify(target_path)
  (cache[key] ||= self.class.index_for(key: key, max_index: max_index)) == index
end

#assign!(path, index) ⇒ Object



23
24
25
26
27
# File 'lib/steep/services/path_assignment.rb', line 23

def assign!(path, index)
  key = stringify(path)
  cache[key] = index
  self
end

#stringify(target_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/steep/services/path_assignment.rb', line 29

def stringify(target_path)
  target =
    case target_path[0]
    when Project::Target
      target_path[0].name.to_s
    else
      target_path[0].to_s
    end
  path = target_path[1].to_s
  "#{target}::#{path}"
end