Class: Eco::API::UseCases::GraphQL::Helpers::Location::TagsRemap::TagsMap

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ TagsMap

Returns a new instance of TagsMap.



6
7
8
9
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 6

def initialize(from, to)
  @from = TagsSet.new(from)
  @to   = TagsSet.new(to)
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 4

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



4
5
6
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 4

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object

Note:

to create a stable sort we assume self is a sorted element



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 16

def <=>(other) # rubocop:disable Metrics/AbcSize
  return -1 if maps?   && !other.maps?
  return  1 if !maps?  && other.maps?
  return -1 if rename? && other.move?
  return  1 if move?   && other.rename?
  return -1 if rename? && other.rename?
  # both are being moved (specific/long mappings first)
  return  1 if from.subset_of?(other.from)
  return -1 if from.superset_of?(other.from)
  return -1 if (from & other.from).empty?
  return -1 if from.length >= other.from.length
  return  1 if from.length <  other.from.length
  -1
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 45

def any?(&block)
  [from, to].any?(&block)
end

#both?(&block) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 41

def both?(&block)
  [from, to].all?(&block)
end

#goes_after?(other) ⇒ Boolean

Note:

to create a stable sort we assume self is a sorted element

Returns:

  • (Boolean)


37
38
39
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 37

def goes_after?(other)
  (self <=> other) == 1
end

#goes_before?(other) ⇒ Boolean

Note:

to create a stable sort we assume self is a sorted element

Returns:

  • (Boolean)


32
33
34
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 32

def goes_before?(other)
  (self <=> other) == -1
end

#maps?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 49

def maps?
  return false if any?(&:empty?)
  return false if from == to
  true
end

#move?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 60

def move?
  return false unless maps?
  !rename?
end

#rename?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 55

def rename?
  return false unless maps?
  both? {|set| set.length == 1}
end

#to_csv_rowObject



11
12
13
# File 'lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb', line 11

def to_csv_row
  [from.to_s, to.to_s]
end