Class: HeadMusic::Notation::StaffPosition

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/notation/staff_position.rb

Constant Summary collapse

NAMES =
{
  -2 => ["ledger line below staff"],
  -1 => ["space below staff"],
  0 => ["bottom line", "line 1"],
  1 => ["bottom space", "space 1"],
  2 => ["line 2"],
  3 => ["space 2"],
  4 => ["middle line", "line 3"],
  5 => ["space 3"],
  6 => ["line 4"],
  7 => ["space 4"],
  8 => ["top line", "line 5"],
  9 => ["space above staff"],
  10 => ["ledger line above staff"]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ StaffPosition

Returns a new instance of StaffPosition.



35
36
37
# File 'lib/head_music/notation/staff_position.rb', line 35

def initialize(index)
  @index = index
end

Instance Attribute Details

#indexObject (readonly)

Integer, even = line, odd = space; bottom line = 0



5
6
7
# File 'lib/head_music/notation/staff_position.rb', line 5

def index
  @index
end

Class Method Details

.name_to_index(name) ⇒ Object

Accepts a name (string or symbol) and returns the corresponding StaffPosition index (integer), or nil if not found



24
25
26
27
28
29
30
31
32
33
# File 'lib/head_music/notation/staff_position.rb', line 24

def self.name_to_index(name)
  NAMES.each do |index, names|
    if names.map { |n|
      HeadMusic::Utilities::Case.to_snake_case(n)
    }.include?(HeadMusic::Utilities::Case.to_snake_case(name))
      return index
    end
  end
  nil
end

Instance Method Details

#line?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/head_music/notation/staff_position.rb', line 39

def line?
  index.even?
end

#line_numberObject



47
48
49
50
# File 'lib/head_music/notation/staff_position.rb', line 47

def line_number
  return nil unless line?
  (index / 2) + 1
end

#space?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/head_music/notation/staff_position.rb', line 43

def space?
  index.odd?
end

#space_numberObject



52
53
54
55
# File 'lib/head_music/notation/staff_position.rb', line 52

def space_number
  return nil unless space?
  ((index - 1) / 2) + 1
end

#to_sObject



57
58
59
60
61
# File 'lib/head_music/notation/staff_position.rb', line 57

def to_s
  return NAMES[index].first if NAMES.key?(index)

  line? ? "line #{line_number}" : "space #{space_number}"
end