Class: HeadMusic::Content::Bar
- Inherits:
-
Object
- Object
- HeadMusic::Content::Bar
- 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
-
#composition ⇒ Object
readonly
Returns the value of attribute composition.
-
#ends_repeat_after_num_plays ⇒ Object
Returns the value of attribute ends_repeat_after_num_plays.
-
#key_signature ⇒ Object
Returns the value of attribute key_signature.
-
#meter ⇒ Object
Returns the value of attribute meter.
-
#plays_on_passes ⇒ Object
Returns the value of attribute plays_on_passes.
-
#starts_repeat ⇒ Object
writeonly
Sets the attribute starts_repeat.
Instance Method Summary collapse
-
#ends_repeat? ⇒ Boolean
-
#initialize(composition, key_signature: nil, meter: nil) ⇒ Bar
constructor
A new instance of Bar.
-
#plays_on_pass?(pass_number) ⇒ Boolean
-
#repeat_summary ⇒ Object
private
-
#starts_repeat? ⇒ Boolean
-
#to_h ⇒ Object
Sparse serialization: only non-default state, so a default bar is {}.
-
#to_s ⇒ Object
-
#valid_ends_repeat_after_num_plays?(value) ⇒ Boolean
private
-
#valid_plays_on_passes?(value) ⇒ Boolean
private
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
#composition ⇒ Object (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_plays ⇒ Object
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_signature ⇒ Object
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 |
#meter ⇒ Object
Returns the value of attribute meter.
8 9 10 |
# File 'lib/head_music/content/bar.rb', line 8 def meter @meter end |
#plays_on_passes ⇒ Object
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
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
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
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_summary ⇒ Object (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
28 29 30 |
# File 'lib/head_music/content/bar.rb', line 28 def starts_repeat? @starts_repeat end |
#to_h ⇒ Object
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_s ⇒ Object
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)
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)
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 |