Class: HeadMusic::Style::Mark

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/style/mark.rb

Overview

A mark is a fragment of music with an optional fitness score assigned. Marks are collected into annotations which comment on a voice.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_position, end_position, placements: [], fitness: nil) ⇒ Mark

Returns a new instance of Mark.



26
27
28
29
30
31
# File 'lib/head_music/style/mark.rb', line 26

def initialize(start_position, end_position, placements: [], fitness: nil)
  @start_position = start_position
  @end_position = end_position
  @placements = [placements].flatten.compact
  @fitness = fitness || HeadMusic::PENALTY_FACTOR
end

Instance Attribute Details

#end_positionObject (readonly)

Returns the value of attribute end_position.



4
5
6
# File 'lib/head_music/style/mark.rb', line 4

def end_position
  @end_position
end

#fitnessObject (readonly)

Returns the value of attribute fitness.



4
5
6
# File 'lib/head_music/style/mark.rb', line 4

def fitness
  @fitness
end

#placementsObject (readonly)

Returns the value of attribute placements.



4
5
6
# File 'lib/head_music/style/mark.rb', line 4

def placements
  @placements
end

#start_positionObject (readonly)

Returns the value of attribute start_position.



4
5
6
# File 'lib/head_music/style/mark.rb', line 4

def start_position
  @start_position
end

Class Method Details

.for(placement, fitness: nil) ⇒ Object



6
7
8
# File 'lib/head_music/style/mark.rb', line 6

def self.for(placement, fitness: nil)
  new(placement.position, placement.next_position, placements: [placement], fitness: fitness)
end

.for_all(placements, fitness: nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/head_music/style/mark.rb', line 10

def self.for_all(placements, fitness: nil)
  placements = [placements].flatten.compact
  return [] if placements.empty?

  start_position = placements.map(&:position).min
  end_position = placements.map(&:next_position).max
  new(start_position, end_position, placements: placements, fitness: fitness)
end

.for_each(placements, fitness: nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/head_music/style/mark.rb', line 19

def self.for_each(placements, fitness: nil)
  placements = [placements].flatten
  placements.map do |placement|
    new(placement.position, placement.next_position, placements: placement, fitness: fitness)
  end
end

Instance Method Details

#codeObject Also known as: to_s



33
34
35
# File 'lib/head_music/style/mark.rb', line 33

def code
  [start_position, end_position].join(" to ")
end