Class: Stave::Core::NoteCollection

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



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

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



4
5
6
# File 'lib/stave/core/note_collection.rb', line 4

def root
  @root
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/stave/core/note_collection.rb', line 4

def type
  @type
end

Class Method Details

.inherited(note_class) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/stave/core/note_collection.rb', line 38

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



34
35
36
# File 'lib/stave/core/note_collection.rb', line 34

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

Instance Method Details

#note_at(position) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/stave/core/note_collection.rb', line 15

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

  position -= 7 while position >= 8

  notes[position - 1]
end

#notesObject



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

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

#to_hObject



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

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

#uniqObject



23
24
25
# File 'lib/stave/core/note_collection.rb', line 23

def uniq
  notes.uniq(&:variant)
end