Class: DiverDown::Definition::Dependency
- Inherits:
-
Object
- Object
- DiverDown::Definition::Dependency
- Includes:
- Comparable
- Defined in:
- lib/diver_down/definition/dependency.rb
Instance Attribute Summary collapse
-
#source_name ⇒ Object
readonly
Returns the value of attribute source_name.
Class Method Summary collapse
- .combine(*dependencies) ⇒ Array<DiverDown::Definition::Dependency>
- .from_hash(hash) ⇒ DiverDown::Definition::Dependency
Instance Method Summary collapse
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eq?, #eql?)
- #find_or_build_method_id(name:, context:) ⇒ DiverDown::Definition::MethodId
- #freeze ⇒ void
- #hash ⇒ Integer
-
#initialize(source_name:, method_ids: []) ⇒ Dependency
constructor
A new instance of Dependency.
- #inspect ⇒ String
- #method_id(name:, context:) ⇒ DiverDown::Definition::MethodId?
- #method_ids ⇒ Array<DiverDown::Definition::MethodId>
- #to_h ⇒ Hash
Constructor Details
#initialize(source_name:, method_ids: []) ⇒ Dependency
Returns a new instance of Dependency.
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_name ⇒ Object (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>
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
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
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?
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
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 |
#freeze ⇒ void
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 |
#hash ⇒ Integer
97 98 99 |
# File 'lib/diver_down/definition/dependency.rb', line 97 def hash [self.class, source_name, method_ids].hash end |
#inspect ⇒ 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?
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_ids ⇒ Array<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_h ⇒ 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 |