Class: DiverDown::Definition::MethodId

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/diver_down/definition/method_id.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, context:, paths: Set.new) ⇒ MethodId

Returns a new instance of MethodId.

Parameters:

  • name (String, Symbol)
  • context ('instance', 'class')
  • paths (Set<String>) (defaults to: Set.new)

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
# File 'lib/diver_down/definition/method_id.rb', line 25

def initialize(name:, context:, paths: Set.new)
  raise ArgumentError, "invalid context: #{context}" unless VALID_CONTEXT.include?(context)

  @name = name.to_s
  @context = context.to_s
  @paths = paths.to_set
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/diver_down/definition/method_id.rb', line 11

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/diver_down/definition/method_id.rb', line 11

def name
  @name
end

#pathsObject (readonly)

Returns the value of attribute paths.



11
12
13
# File 'lib/diver_down/definition/method_id.rb', line 11

def paths
  @paths
end

Class Method Details

.from_hash(hash) ⇒ Object

Parameters:

  • hash (Hash)


14
15
16
17
18
19
20
# File 'lib/diver_down/definition/method_id.rb', line 14

def self.from_hash(hash)
  new(
    name: hash[:name] || hash['name'],
    context: hash[:context] || hash['context'],
    paths: hash[:paths] || hash['paths'] || []
  )
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


62
63
64
# File 'lib/diver_down/definition/method_id.rb', line 62

def <=>(other)
  [context, name] <=> [other.context, other.name]
end

#==(other) ⇒ Boolean Also known as: eq?, eql?

Parameters:

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/diver_down/definition/method_id.rb', line 68

def ==(other)
  other.is_a?(self.class) &&
    name == other.name &&
    context == other.context &&
    paths == other.paths
end

#add_path(*paths) ⇒ Object

Parameters:

  • path (Array<String>)


34
35
36
37
38
# File 'lib/diver_down/definition/method_id.rb', line 34

def add_path(*paths)
  paths.each do
    @paths.add(_1)
  end
end

#freezevoid

This method returns an undefined value.



83
84
85
86
# File 'lib/diver_down/definition/method_id.rb', line 83

def freeze
  super
  @paths.freeze
end

#hashInteger

Returns:

  • (Integer)


56
57
58
# File 'lib/diver_down/definition/method_id.rb', line 56

def hash
  [self.class, name, context, paths].hash
end

#human_method_nameString

Returns:

  • (String)


41
42
43
44
# File 'lib/diver_down/definition/method_id.rb', line 41

def human_method_name
  prefix = context == 'instance' ? '#' : '.'
  "#{prefix}#{name}"
end

#inspectString

Returns:

  • (String)


78
79
80
# File 'lib/diver_down/definition/method_id.rb', line 78

def inspect
  %(#<#{self.class} #{human_method_name} paths=#{paths.inspect}>")
end

#to_hHash

Returns:

  • (Hash)


47
48
49
50
51
52
53
# File 'lib/diver_down/definition/method_id.rb', line 47

def to_h
  {
    name:,
    context:,
    paths: @paths.sort,
  }
end