Class: DiverDown::Definition::Dependency

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_name:, method_ids: []) ⇒ Dependency

Returns a new instance of Dependency.

Parameters:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/diver_down/definition/dependency.rb', line 42

def initialize(source_name:, method_ids: [])
  @source_name = source_name
  @method_id_map = {
    'class' => {},
    'instance' => {},
  }

  method_ids.each do |method_id|
    @method_id_map[method_id.context][method_id.name] = method_id
  end
end

Instance Attribute Details

#source_nameObject (readonly)

Returns the value of attribute source_name.



38
39
40
# File 'lib/diver_down/definition/dependency.rb', line 38

def source_name
  @source_name
end

Class Method Details

.combine(*dependencies) ⇒ Array<DiverDown::Definition::Dependency>

Parameters:

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/diver_down/definition/dependency.rb', line 23

def self.combine(*dependencies)
  dependencies.group_by(&:source_name).map do |source_name, same_source_dependencies|
    new_dependency = new(source_name:)

    same_source_dependencies.each do |dependency|
      dependency.method_ids.each do |method_id|
        new_method_id = new_dependency.find_or_build_method_id(name: method_id.name, context: method_id.context)
        new_method_id.add_path(*method_id.paths)
      end
    end

    new_dependency
  end
end

.from_hash(hash) ⇒ DiverDown::Definition::Dependency

Parameters:

  • hash (Hash)

Returns:



10
11
12
13
14
15
16
17
18
19
# File 'lib/diver_down/definition/dependency.rb', line 10

def self.from_hash(hash)
  method_ids = (hash[:method_ids] || hash['method_ids'] || []).map do
    DiverDown::Definition::MethodId.from_hash(_1)
  end

  new(
    source_name: hash[:source_name] || hash['source_name'],
    method_ids:
  )
end

Instance Method Details

#<=>(other) ⇒ Integer

Returns:

  • (Integer)


82
83
84
# File 'lib/diver_down/definition/dependency.rb', line 82

def <=>(other)
  source_name <=> other.source_name
end

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

Parameters:

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/diver_down/definition/dependency.rb', line 88

def ==(other)
  other.is_a?(self.class) &&
    source_name == other.source_name &&
    method_ids == other.method_ids
end

#find_or_build_method_id(name:, context:) ⇒ DiverDown::Definition::MethodId

Parameters:

  • name (String)
  • context ('instance', 'class')

Returns:



57
58
59
# File 'lib/diver_down/definition/dependency.rb', line 57

def find_or_build_method_id(name:, context:)
  @method_id_map[context.to_s][name.to_s] ||= DiverDown::Definition::MethodId.new(name:, context:)
end

#freezevoid

This method returns an undefined value.



107
108
109
110
111
112
113
114
# File 'lib/diver_down/definition/dependency.rb', line 107

def freeze
  super
  @method_id_map.transform_values do
    _1.transform_values(&:freeze)
    _1.freeze
  end
  @method_id_map.freeze
end

#hashInteger

Returns:

  • (Integer)


97
98
99
# File 'lib/diver_down/definition/dependency.rb', line 97

def hash
  [self.class, source_name, method_ids].hash
end

#inspectString

Returns:

  • (String)


102
103
104
# File 'lib/diver_down/definition/dependency.rb', line 102

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

#method_id(name:, context:) ⇒ DiverDown::Definition::MethodId?

Parameters:

  • name (String, Symbol)
  • context ('instance', 'class')

Returns:



64
65
66
# File 'lib/diver_down/definition/dependency.rb', line 64

def method_id(name:, context:)
  @method_id_map[context.to_s][name.to_s]
end

#method_idsArray<DiverDown::Definition::MethodId>



69
70
71
# File 'lib/diver_down/definition/dependency.rb', line 69

def method_ids
  (@method_id_map['class'].values + @method_id_map['instance'].values).sort
end

#to_hHash

Returns:

  • (Hash)


74
75
76
77
78
79
# File 'lib/diver_down/definition/dependency.rb', line 74

def to_h
  {
    source_name:,
    method_ids: method_ids.map(&:to_h),
  }
end