Class: HeadMusic::Content::StaffSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/content/staff_system.rb

Overview

An ordered set of staves joined by a brace or a bracket: a piano's two, a harp's two, an organ's three, or the single staff most parts use.

Constant Summary collapse

BRACKETS =
%i[brace bracket none].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(staves: nil, bracket: :none) ⇒ StaffSystem

Returns a new instance of StaffSystem.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'lib/head_music/content/staff_system.rb', line 11

def initialize(staves: nil, bracket: :none)
  raise ArgumentError, "bracket must be one of: #{BRACKETS.join(", ")}" unless BRACKETS.include?(bracket)

  @staves = Array(staves)
  @staves = [HeadMusic::Content::Staff.new] if @staves.empty?
  @bracket = bracket
end

Instance Attribute Details

#bracket ⇒ Object (readonly)

Returns the value of attribute bracket.



9
10
11
# File 'lib/head_music/content/staff_system.rb', line 9

def bracket
  @bracket
end

#staves ⇒ Object (readonly)

Returns the value of attribute staves.



9
10
11
# File 'lib/head_music/content/staff_system.rb', line 9

def staves
  @staves
end

Class Method Details

.grand_staff ⇒ Object

A grand staff, treble over bass, as a piano or harp uses.



25
26
27
28
# File 'lib/head_music/content/staff_system.rb', line 25

def self.grand_staff
  new(staves: [HeadMusic::Content::Staff.new(clef: :treble_clef), HeadMusic::Content::Staff.new(clef: :bass_clef)],
    bracket: :brace)
end

.single_staff(clef: nil) ⇒ Object

The one-staff system a part falls back to when nothing has said otherwise.



20
21
22
# File 'lib/head_music/content/staff_system.rb', line 20

def self.single_staff(clef: nil)
  new(staves: [HeadMusic::Content::Staff.new(clef: clef)])
end

Instance Method Details

#first_staff ⇒ Object



30
31
32
# File 'lib/head_music/content/staff_system.rb', line 30

def first_staff
  staves.first
end

#include?(staff) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/head_music/content/staff_system.rb', line 34

def include?(staff)
  staves.any? { |candidate| candidate.equal?(staff) }
end

#length ⇒ Object



38
39
40
# File 'lib/head_music/content/staff_system.rb', line 38

def length
  staves.length
end

#to_h ⇒ Object



46
47
48
# File 'lib/head_music/content/staff_system.rb', line 46

def to_h
  {"bracket" => bracket.to_s, "staves" => staves.map(&:to_h)}
end

#to_s ⇒ Object



42
43
44
# File 'lib/head_music/content/staff_system.rb', line 42

def to_s
  "#{length}-staff #{bracket}"
end