Class: DiverDown::Definition

Inherits:
Object
  • Object
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition_group: nil, title: '', sources: []) ⇒ Definition

Returns a new instance of Definition.

Parameters:



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_groupObject (readonly)

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_idObject

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

#titleObject (readonly)

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

Parameters:

  • definition_group (String, nil)
  • title (String)
  • definitions (Array<DiverDown::Definition>) (defaults to: [])


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

.from_hash(hash) ⇒ DiverDown::Definition

Parameters:

  • hash (Hash)

Returns:



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?

Parameters:

Returns:

  • (Boolean)


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

#find_or_build_source(source_name) ⇒ DiverDown::Definition::Source

Parameters:

  • source_name (String)

Returns:



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

#freezevoid

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

#hashInteger

Returns:

  • (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

#source(source_name) ⇒ DiverDown::Definition::Source?

Parameters:

  • source_name (String)

Returns:



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

def source(source_name)
  @source_map[source_name]
end

#sourcesArray<DiverDown::Definition::Source>

Returns:



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

def sources
  @source_map.values.sort
end

#to_hString

Returns:

  • (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