Class: Stave::Core::NoteCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stave/core/note_collection.rb

Direct Known Subclasses

Theory::Chord, Theory::Mode, Theory::Scale

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, type:) ⇒ NoteCollection

Returns a new instance of NoteCollection.



10
11
12
13
# File 'lib/stave/core/note_collection.rb', line 10

def initialize(root:, type:)
  @root = root
  @type = type
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/stave/core/note_collection.rb', line 8

def root
  @root
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/stave/core/note_collection.rb', line 8

def type
  @type
end

Class Method Details

.inherited(note_class) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/stave/core/note_collection.rb', line 58

def self.inherited(note_class)
  note_class.type_class.variants.each do |type|
    self.class.define_method(
      :"#{type.variant}_from", ->(root) { new(type:, root:) }
    )
  end
end

.type_classObject



54
55
56
# File 'lib/stave/core/note_collection.rb', line 54

def self.type_class
  const_get("#{self}Type")
end

Instance Method Details

#degree_note(degree) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/stave/core/note_collection.rb', line 35

def degree_note(degree)
  return unless degrees.include?(degree)

  position = degrees.index(degree)

  notes[position]
end

#note_at(position) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/stave/core/note_collection.rb', line 19

def note_at(position)
  return if position.zero? || position.negative?

  position -= 7 while position >= 8

  notes[position - 1]
end

#note_degree(note) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/stave/core/note_collection.rb', line 27

def note_degree(note)
  return unless notes.include?(note)

  position = notes.index(note)

  degrees[position]
end

#notesObject



15
16
17
# File 'lib/stave/core/note_collection.rb', line 15

def notes
  type.intervals.map { |interval| root + interval }
end

#to_hObject



47
48
49
50
51
52
# File 'lib/stave/core/note_collection.rb', line 47

def to_h
  {
    self.class.type_class.class_key => { variant: type.variant },
    root: root.variant
  }
end

#uniqObject



43
44
45
# File 'lib/stave/core/note_collection.rb', line 43

def uniq
  notes.uniq(&:variant)
end