Class: DiverDown::Definition
- Inherits:
-
Object
- Object
- DiverDown::Definition
show all
- Defined in:
- lib/diver_down/definition.rb,
lib/diver_down/definition/source.rb,
lib/diver_down/definition/method_id.rb,
lib/diver_down/definition/dependency.rb
Defined Under Namespace
Classes: Dependency, MethodId, Source
Instance Attribute Summary collapse
-
#definition_group ⇒ Object
readonly
Returns the value of attribute definition_group.
-
#store_id ⇒ Object
ID issued when stored in DiverDown::Web::DefinitionStore I want to manage ID in DiverDown::Web::DefinitionStore, but for performance reasons, I have to set Definition#id to determine its identity because naive comparing the identity by instance variables of Definitions is slow.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(definition_group: nil, title: '', sources: []) ⇒ Definition
Returns a new instance of Definition.
50
51
52
53
54
|
# File 'lib/diver_down/definition.rb', line 50
def initialize(definition_group: nil, title: '', sources: [])
@definition_group = definition_group
@title = title
@source_map = sources.map { [_1.source_name, _1] }.to_h
end
|
Instance Attribute Details
#definition_group ⇒ Object
Returns the value of attribute definition_group.
40
41
42
|
# File 'lib/diver_down/definition.rb', line 40
def definition_group
@definition_group
end
|
#store_id ⇒ Object
ID issued when stored in DiverDown::Web::DefinitionStore I want to manage ID in DiverDown::Web::DefinitionStore, but for performance reasons, I have to set Definition#id to determine its identity because naive comparing the identity by instance variables of Definitions is slow.
46
47
48
|
# File 'lib/diver_down/definition.rb', line 46
def store_id
@store_id
end
|
#title ⇒ Object
Returns the value of attribute title.
40
41
42
|
# File 'lib/diver_down/definition.rb', line 40
def title
@title
end
|
Class Method Details
.combine(definition_group:, title:, definitions: []) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/diver_down/definition.rb', line 26
def self.combine(definition_group:, title:, definitions: [])
all_sources = definitions.flat_map(&:sources)
sources = all_sources.group_by(&:source_name).map do |_, same_sources|
DiverDown::Definition::Source.combine(*same_sources)
end
new(
definition_group:,
title:,
sources:
)
end
|
13
14
15
16
17
18
19
20
21
|
# File 'lib/diver_down/definition.rb', line 13
def self.from_hash(hash)
new(
title: hash[:title] || hash['title'] || '',
definition_group: hash[:definition_group] || hash['definition_group'],
sources: (hash[:sources] || hash['sources'] || []).map do |source_hash|
DiverDown::Definition::Source.from_hash(source_hash)
end
)
end
|
Instance Method Details
#==(other) ⇒ Boolean
Also known as:
eq?, eql?
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/diver_down/definition.rb', line 84
def ==(other)
if store_id
other.is_a?(self.class) &&
store_id == other.store_id
else
other.is_a?(self.class) &&
definition_group == other.definition_group &&
title == other.title &&
sources.sort == other.sources.sort
end
end
|
58
59
60
|
# File 'lib/diver_down/definition.rb', line 58
def find_or_build_source(source_name)
@source_map[source_name] ||= DiverDown::Definition::Source.new(source_name:)
end
|
#freeze ⇒ void
This method returns an undefined value.
108
109
110
111
112
113
|
# File 'lib/diver_down/definition.rb', line 108
def freeze
super
@source_map.transform_values(&:freeze)
@source_map.freeze
end
|
#hash ⇒ Integer
99
100
101
102
103
104
105
|
# File 'lib/diver_down/definition.rb', line 99
def hash
if store_id
[self.class, store_id].hash
else
[self.class, definition_group, title, sources].hash
end
end
|
64
65
66
|
# File 'lib/diver_down/definition.rb', line 64
def source(source_name)
@source_map[source_name]
end
|
69
70
71
|
# File 'lib/diver_down/definition.rb', line 69
def sources
@source_map.values.sort
end
|
#to_h ⇒ String
74
75
76
77
78
79
80
|
# File 'lib/diver_down/definition.rb', line 74
def to_h
{
definition_group:,
title:,
sources: sources.map(&:to_h),
}
end
|