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_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.
12 13 14 15 16 17 18 19 |
# File 'lib/head_music/content/bar.rb', line 12 def initialize(composition, key_signature: nil, meter: nil) @composition = composition @key_signature = HeadMusic::Rudiment::KeySignature.get(key_signature) if key_signature @meter = HeadMusic::Rudiment::Meter.get(meter) if 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.
9 10 11 |
# File 'lib/head_music/content/bar.rb', line 9 def key_signature @key_signature end |
#meter ⇒ Object
Returns the value of attribute meter.
9 10 11 |
# File 'lib/head_music/content/bar.rb', line 9 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
10 11 12 |
# File 'lib/head_music/content/bar.rb', line 10 def starts_repeat=(value) @starts_repeat = value end |
Instance Method Details
#ends_repeat? ⇒ Boolean
32 33 34 |
# File 'lib/head_music/content/bar.rb', line 32 def ends_repeat? !ends_repeat_after_num_plays.nil? end |
#plays_on_pass?(pass_number) ⇒ Boolean
43 44 45 |
# File 'lib/head_music/content/bar.rb', line 43 def plays_on_pass?(pass_number) plays_on_passes.nil? || plays_on_passes.include?(pass_number) end |
#repeat_summary ⇒ Object (private)
67 68 69 70 71 72 73 |
# File 'lib/head_music/content/bar.rb', line 67 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
21 22 23 |
# File 'lib/head_music/content/bar.rb', line 21 def starts_repeat? @starts_repeat end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/head_music/content/bar.rb', line 47 def to_s ["Bar", key_signature, meter, repeat_summary].compact.join(" ") end |
#valid_ends_repeat_after_num_plays?(value) ⇒ Boolean (private)
53 54 55 56 57 |
# File 'lib/head_music/content/bar.rb', line 53 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)
59 60 61 62 63 64 65 |
# File 'lib/head_music/content/bar.rb', line 59 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 |