Class: Gitara::Node::Base

Inherits:
Valuable
  • Object
show all
Includes:
Redwood
Defined in:
lib/gitara/node/base.rb,
lib/gitara/node/base/node_version.rb,
lib/gitara/node/base/stanza_version.rb,
lib/gitara/node/base/voiced_version.rb,
lib/gitara/node/base/chorded_version.rb

Direct Known Subclasses

Alternative, Bar, ChordSet, Line, NoteSet, Repeat, Score, Stanza, Tab

Defined Under Namespace

Classes: ChordedVersion, NodeVersion, StanzaVersion, VoicedVersion

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#$1($1) ⇒ Object



8
# File 'lib/gitara/node/base.rb', line 8

has_value :name

Instance Method Details

#add(child) ⇒ Object



12
13
14
15
# File 'lib/gitara/node/base.rb', line 12

def add(child)
  child.id = children.select{|c| c.is_a?(child.class)}.size + 1
  graft child
end

#ancestor(klass) ⇒ Object



17
18
19
# File 'lib/gitara/node/base.rb', line 17

def ancestor(klass)
  ancestors.detect{|ancestor| ancestor.is_a?(klass)}
end

#call_value(node_version) ⇒ Object



21
22
23
# File 'lib/gitara/node/base.rb', line 21

def call_value(node_version)
  node_version.call_name
end

#children=(values) ⇒ Object



33
34
35
36
37
38
# File 'lib/gitara/node/base.rb', line 33

def children=(values)
  children.clear
  values.each do |child|
    add child
  end
end

#chordedObject



40
41
42
# File 'lib/gitara/node/base.rb', line 40

def chorded
  self.class::ChordedVersion.new(:node => self)
end

#definition(target = self) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/gitara/node/base.rb', line 44

def definition(target = self)
  if self.definition_of?(target)
    self
  else
    result = parent.children.detect{|node| node.definition_of?(target) }
    result ? result : parent.definition(target)
  end
end

#definition?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/gitara/node/base.rb', line 53

def definition?
  has_children? || ! value.nil?
end

#definition_childrenObject



25
26
27
28
29
30
31
# File 'lib/gitara/node/base.rb', line 25

def definition_children
  if leaf?
    definition ? definition.children : []
  else
    children
  end
end

#definition_nameObject



57
58
59
60
61
62
# File 'lib/gitara/node/base.rb', line 57

def definition_name
  name.to_s.
    gsub(/\W/, '_').
    gsub(/\d+/){|s| Utilities.id_as_word(s)}.
    camelize
end

#definition_of?(target) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/gitara/node/base.rb', line 64

def definition_of?(target)
  self.definition? && self.name == target.name && self.class == target.class
end

#definitions(klass) ⇒ Object



68
69
70
# File 'lib/gitara/node/base.rb', line 68

def definitions(klass)
  self.is_a?(klass) && self.definition? ? [self] : self.children.map{|child| child.definitions(klass) }.flatten
end

#descendants(klass) ⇒ Object



72
73
74
# File 'lib/gitara/node/base.rb', line 72

def descendants(klass)
  self.is_a?(klass) ? [self.definition] : self.definition_children.map{|child| child.descendants(klass) }.flatten
end

#idObject



9
# File 'lib/gitara/node/base.rb', line 9

has_value :id, :default => 1

#id_as_wordObject



76
77
78
# File 'lib/gitara/node/base.rb', line 76

def id_as_word
  Utilities.id_as_word(id)
end

#inspectObject



80
81
82
# File 'lib/gitara/node/base.rb', line 80

def inspect
  Utilities.inspect_attributes self, :name
end

#nameObject



8
# File 'lib/gitara/node/base.rb', line 8

has_value :name

#stanza_versionObject



88
89
90
# File 'lib/gitara/node/base.rb', line 88

def stanza_version
  self.class::StanzaVersion.new(:node => self)
end

#valueObject



10
# File 'lib/gitara/node/base.rb', line 10

has_value :value

#voiced_as(arg) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/gitara/node/base.rb', line 96

def voiced_as(arg)
  if arg.is_a?(Voice)
    self.class::VoicedVersion.new(:node => self, :voice => arg)
  elsif arg.is_a?(Array)
    arg.map{|voice| self.voiced_as(voice) }
  else
    raise ArgumentError
  end
end