Class: DiverDown::Definition::Source

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_name:, dependencies: []) ⇒ Source

Returns a new instance of Source.

Parameters:



36
37
38
39
# File 'lib/diver_down/definition/source.rb', line 36

def initialize(source_name:, dependencies: [])
  @source_name = source_name
  @dependency_map = dependencies.map { [_1.source_name, _1] }.to_h
end

Instance Attribute Details

#source_nameObject (readonly)

Returns the value of attribute source_name.



32
33
34
# File 'lib/diver_down/definition/source.rb', line 32

def source_name
  @source_name
end

Class Method Details

.combine(*sources) ⇒ DiverDown::Definition::Source

Parameters:

Returns:

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/diver_down/definition/source.rb', line 18

def self.combine(*sources)
  raise ArgumentError, 'sources are empty' if sources.empty?

  unique_sources = sources.map(&:source_name).uniq
  raise ArgumentError, "sources are unmatched. (#{unique_sources})" unless unique_sources.length == 1

  all_dependencies = sources.flat_map(&:dependencies)

  new(
    source_name: unique_sources[0],
    dependencies: DiverDown::Definition::Dependency.combine(*all_dependencies)
  )
end

.from_hash(hash) ⇒ Object

Parameters:

  • hash (Hash)


9
10
11
12
13
14
# File 'lib/diver_down/definition/source.rb', line 9

def self.from_hash(hash)
  new(
    source_name: hash[:source_name] || hash['source_name'],
    dependencies: (hash[:dependencies] || hash['dependencies'] || []).map { DiverDown::Definition::Dependency.from_hash(_1) }
  )
end

Instance Method Details

#<=>(other) ⇒ Integer

Parameters:

Returns:

  • (Integer)


76
77
78
# File 'lib/diver_down/definition/source.rb', line 76

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

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

Parameters:

Returns:

  • (Boolean)


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

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

#delete_dependency(dependency_source_name) ⇒ void

This method returns an undefined value.

Parameters:

  • dependency_source_name (String)


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

def delete_dependency(dependency_source_name)
  @dependency_map.delete(dependency_source_name)
end

#dependenciesArray<DiverDown::Definition::Dependency>



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

def dependencies
  @dependency_map.values.sort
end

#dependency(dependency_source_name) ⇒ DiverDown::Definition::Dependency?

Parameters:

  • dependency_source_name (String)

Returns:



51
52
53
# File 'lib/diver_down/definition/source.rb', line 51

def dependency(dependency_source_name)
  @dependency_map[dependency_source_name]
end

#find_or_build_dependency(dependency_source_name) ⇒ DiverDown::Definition::Dependency?

Return nil if source is self.source

Parameters:

  • source (String)

Returns:



43
44
45
46
47
# File 'lib/diver_down/definition/source.rb', line 43

def find_or_build_dependency(dependency_source_name)
  return if source_name == dependency_source_name

  @dependency_map[dependency_source_name] ||= DiverDown::Definition::Dependency.new(source_name: dependency_source_name)
end

#freezevoid

This method returns an undefined value.



96
97
98
99
100
# File 'lib/diver_down/definition/source.rb', line 96

def freeze
  super
  @dependency_map.transform_values(&:freeze)
  @dependency_map.freeze
end

#hashInteger

Returns:

  • (Integer)


91
92
93
# File 'lib/diver_down/definition/source.rb', line 91

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

#to_hHash

Returns:

  • (Hash)


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

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