Class: DiverDown::Definition::Source
- Inherits:
-
Object
- Object
- DiverDown::Definition::Source
- Includes:
- Comparable
- Defined in:
- lib/diver_down/definition/source.rb
Instance Attribute Summary collapse
-
#source_name ⇒ Object
readonly
Returns the value of attribute source_name.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Integer
- #==(other) ⇒ Boolean (also: #eq?, #eql?)
- #delete_dependency(dependency_source_name) ⇒ void
- #dependencies ⇒ Array<DiverDown::Definition::Dependency>
- #dependency(dependency_source_name) ⇒ DiverDown::Definition::Dependency?
-
#find_or_build_dependency(dependency_source_name) ⇒ DiverDown::Definition::Dependency?
Return nil if source is self.source.
- #freeze ⇒ void
- #hash ⇒ Integer
-
#initialize(source_name:, dependencies: []) ⇒ Source
constructor
A new instance of Source.
- #to_h ⇒ Hash
Constructor Details
#initialize(source_name:, dependencies: []) ⇒ Source
Returns a new instance of Source.
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_name ⇒ Object (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
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
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
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?
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.
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 |
#dependencies ⇒ Array<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?
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
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 |
#freeze ⇒ void
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 |
#hash ⇒ Integer
91 92 93 |
# File 'lib/diver_down/definition/source.rb', line 91 def hash [self.class, source_name, dependencies].hash end |
#to_h ⇒ 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 |