Class: KalibroClient::Entities::Miscellaneous::Granularity

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/kalibro_client/entities/miscellaneous/granularity.rb

Constant Summary collapse

GRANULARITIES =
[:METHOD, :CLASS, :PACKAGE, :SOFTWARE, :FUNCTION]
PARENTS =
{
    METHOD: CLASS,
    CLASS: PACKAGE,
    PACKAGE: SOFTWARE,
    SOFTWARE: SOFTWARE,
    FUNCTION: PACKAGE
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Granularity

Returns a new instance of Granularity.



23
24
25
26
27
28
29
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 23

def initialize(type)
  if GRANULARITIES.include?(type)
    @type = type
  else
    raise TypeError.new("Not supported granularity type #{type}")
  end
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 21

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object

FYI: this is a spaceship operator



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 44

def <=>(other)
  return nil if [[:FUNCTION, :METHOD], [:METHOD, :FUNCTION], [:FUNCTION, :CLASS], [:CLASS, :FUNCTION]].include?([self.type, other.type])

  if self.type == other.type
    return 0
  elsif self.is_lower_than?(other.type)
    return -1
  else
    return 1
  end
end

#is_lower_than?(other_type) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 56

def is_lower_than?(other_type)
  current_type = self.type

  while current_type != PARENTS[current_type]
    return true if PARENTS[current_type] == other_type
    current_type = PARENTS[current_type]
  end

  return false
end

#parentObject

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 31

def parent
  parent_type = PARENTS[self.type]
  raise ArgumentError.new("Not supported granularity type #{type}") if parent_type.nil?

  return self if self.type == parent_type
  Granularity.new(parent_type)
end

#to_sObject



39
40
41
# File 'lib/kalibro_client/entities/miscellaneous/granularity.rb', line 39

def to_s
  self.type.to_s
end