Class: Alda::Part

Inherits:
Event
  • Object
show all
Defined in:
lib/alda-rb/event.rb

Overview

A part event. An Alda::EventContainer containing an Alda::Part can be derived using event list sugar. See Alda::EventList#method_missing.

A part can have nickname.

Alda::Score.new do
  piano_ 'player1'
  c4 d e e e1
  piano_ 'player2'
  e4 d g g g1
end

You can use Alda::EventContainer#/ to have a set of instruments.

Alda::Score.new do
  violin_/viola_
  c2 d4 e2_4
end

A set of instruments can also have nickname. You can also access an instruments from a set. See #method_missing.

Alda::Score.new do
  violin_/viola_/cello_('strings')
  g1_1_1
  strings_.cello_
  c1_1_1
end

Instance Attribute Summary collapse

Attributes inherited from Event

#container, #parent

Instance Method Summary collapse

Methods inherited from Event

#detach_from_parent, #is_event_of?, #on_contained

Constructor Details

#initialize(names, arg = nil) ⇒ Part

:call-seq:

new(names, arg=nil) -> Alda::Part

Creates an Alda::Part.



846
847
848
849
850
# File 'lib/alda-rb/event.rb', line 846

def initialize names, arg = nil
	super()
	@names = names.map { Alda::Utils.snake_to_slug _1 }
	@arg = arg
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

:call-seq:

part.(component)_() -> Alda::EventContainer or Alda::Part

Enables dot accessor.

Alda::Score.new do
  violin_/viola_/cello_('strings'); g1_1_1
  strings_.cello_; -o; c1_1_1
end.play


873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/alda-rb/event.rb', line 873

def method_missing name, *args
	str = name.to_s
	return super unless str[-1] == ?_
	str[-1] = ''
	@names.last.concat ?., str
	if args.size == 1
		arg = args.first.tap &:detach_from_parent
		detach_from_parent
		container = Alda::EventContainer.new Alda::Sequence.join(self, arg), @parent
		@parent.events.push container
		container
	else
		@container || self
	end
end

Instance Attribute Details

#argObject

The nickname of the part. nil if none.



839
840
841
# File 'lib/alda-rb/event.rb', line 839

def arg
  @arg
end

#namesObject

The names of the part. To be joined with / as delimiter.



835
836
837
# File 'lib/alda-rb/event.rb', line 835

def names
  @names
end

Instance Method Details

#==(other) ⇒ Object

:call-seq:

part == other -> true or false

Overrides Alda::Event#==. Returns true if other is an Alda::Part and has the same #names and #arg as part (using ==).



896
897
898
# File 'lib/alda-rb/event.rb', line 896

def == other
	super || other.is_a?(Alda::Part) && @names == other.names && @arg == other.arg
end

#to_alda_codeObject

:call-seq:

to_alda_code() -> String

Overrides Alda::Event#to_alda_code.



857
858
859
860
861
# File 'lib/alda-rb/event.rb', line 857

def to_alda_code
	result = @names.join ?/
	result.concat " \"#{@arg}\"" if @arg
	result.concat ?:
end