Class: HeadMusic::Content::Bar

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

Overview

Representation of a bar in a composition Encapsulates meter and key signature changes and repeat structure (repeat barlines and volta brackets) as content semantics

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composition, key_signature: nil, meter: nil) ⇒ Bar

Returns a new instance of Bar.



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

def initialize(composition, key_signature: nil, meter: nil)
  @composition = composition
  self.key_signature = key_signature
  self.meter = meter
  @starts_repeat = false
  @ends_repeat_after_num_plays = nil
  @plays_on_passes = nil
end

Instance Attribute Details

#compositionObject (readonly)

Returns the value of attribute composition.



8
9
10
# File 'lib/head_music/content/bar.rb', line 8

def composition
  @composition
end

#ends_repeat_after_num_playsObject

Returns the value of attribute ends_repeat_after_num_plays.



8
9
10
# File 'lib/head_music/content/bar.rb', line 8

def ends_repeat_after_num_plays
  @ends_repeat_after_num_plays
end

#key_signatureObject

Returns the value of attribute key_signature.



8
9
10
# File 'lib/head_music/content/bar.rb', line 8

def key_signature
  @key_signature
end

#meterObject

Returns the value of attribute meter.



8
9
10
# File 'lib/head_music/content/bar.rb', line 8

def meter
  @meter
end

#plays_on_passesObject

Returns the value of attribute plays_on_passes.



8
9
10
# File 'lib/head_music/content/bar.rb', line 8

def plays_on_passes
  @plays_on_passes
end

#starts_repeat=(value) ⇒ Object (writeonly)

Sets the attribute starts_repeat

Parameters:

  • value

    the value to set the attribute starts_repeat to.



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

def starts_repeat=(value)
  @starts_repeat = value
end

Instance Method Details

#ends_repeat?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/head_music/content/bar.rb', line 39

def ends_repeat?
  !ends_repeat_after_num_plays.nil?
end

#plays_on_pass?(pass_number) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/head_music/content/bar.rb', line 50

def plays_on_pass?(pass_number)
  plays_on_passes.nil? || plays_on_passes.include?(pass_number)
end

#repeat_summaryObject (private)



87
88
89
90
91
92
93
# File 'lib/head_music/content/bar.rb', line 87

def repeat_summary
  parts = []
  parts << "|:" if starts_repeat?
  parts << ":|x#{ends_repeat_after_num_plays}" if ends_repeat?
  parts << "(passes #{plays_on_passes.join(",")})" if plays_on_passes
  parts.join(" ") unless parts.empty?
end

#starts_repeat?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/head_music/content/bar.rb', line 28

def starts_repeat?
  @starts_repeat
end

#to_hObject

Sparse serialization: only non-default state, so a default bar is {}. KeySignature serializes via #name (“F♯ minor”) because #to_s (“3 sharps”) cannot be parsed back by KeySignature.get.



61
62
63
64
65
66
67
68
69
# File 'lib/head_music/content/bar.rb', line 61

def to_h
  hash = {}
  hash["key_signature"] = key_signature.name if key_signature
  hash["meter"] = meter.to_s if meter
  hash["starts_repeat"] = true if starts_repeat?
  hash["ends_repeat_after_num_plays"] = ends_repeat_after_num_plays if ends_repeat?
  hash["plays_on_passes"] = plays_on_passes.dup if plays_on_passes
  hash
end

#to_sObject



54
55
56
# File 'lib/head_music/content/bar.rb', line 54

def to_s
  ["Bar", key_signature, meter, repeat_summary].compact.join(" ")
end

#valid_ends_repeat_after_num_plays?(value) ⇒ Boolean (private)

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/head_music/content/bar.rb', line 73

def valid_ends_repeat_after_num_plays?(value)
  return true if value.nil?

  value.is_a?(Integer) && value >= 2
end

#valid_plays_on_passes?(value) ⇒ Boolean (private)

Returns:

  • (Boolean)


79
80
81
82
83
84
85
# File 'lib/head_music/content/bar.rb', line 79

def valid_plays_on_passes?(value)
  return true if value.nil?

  value.is_a?(Array) && !value.empty? &&
    value.all? { |pass| pass.is_a?(Integer) && pass.positive? } &&
    value.uniq.length == value.length
end