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.



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

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.



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

def end_position
  @end_position
end

#fitnessObject (readonly)

Returns the value of attribute fitness.



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

def fitness
  @fitness
end

#placementsObject (readonly)

Returns the value of attribute placements.



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

def placements
  @placements
end

#start_positionObject (readonly)

Returns the value of attribute start_position.



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

def start_position
  @start_position
end

Class Method Details

.for(placement, fitness: nil) ⇒ Object



8
9
10
# File 'lib/head_music/style/mark.rb', line 8

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

.for_all(placements, fitness: nil) ⇒ Object



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

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



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

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



35
36
37
# File 'lib/head_music/style/mark.rb', line 35

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

#to_sObject



39
40
41
# File 'lib/head_music/style/mark.rb', line 39

def to_s
  code
end