Skip to content

Commit

Permalink
Removing to_yaml() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrait committed Apr 19, 2014
1 parent 9657ea0 commit 691fffb
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 82 deletions.
18 changes: 0 additions & 18 deletions lib/beats/pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,6 @@ def same_tracks_as?(other_pattern)
@tracks.length == other_pattern.tracks.length
end

# Returns a YAML representation of the Pattern. Produces nicer looking output than the default
# version of to_yaml().
def to_yaml
longest_track_name_length =
@tracks.keys.inject(0) do |max_length, name|
(name.to_s.length > max_length) ? name.to_s.length : max_length
end
ljust_amount = longest_track_name_length + 7

yaml = "#{@name.to_s.capitalize}:\n"
@tracks.keys.sort.each do |track_name|
yaml += " - #{track_name}:".ljust(ljust_amount)
yaml += "#{@tracks[track_name].rhythm}\n"
end

yaml
end

attr_accessor :tracks, :name

private
Expand Down
48 changes: 0 additions & 48 deletions lib/beats/song.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,6 @@ def remove_unused_patterns
@patterns.reject! {|k, pattern| !@flow.member?(pattern.name) }
end

# Serializes the current Song to a YAML string. This string can then be used to construct a new Song
# using the SongParser class. This lets you save a Song to disk, to be re-loaded later. Produces nicer
# looking output than the default version of to_yaml().
def to_yaml(kit)
# This implementation intentionally builds up a YAML string manually instead of using YAML::dump().
# Ruby 1.8 makes it difficult to ensure a consistent ordering of hash keys, which makes the output ugly
# and also hard to test.

yaml_output = "Song:\n"
yaml_output += " Tempo: #{@tempo}\n"
yaml_output += flow_to_yaml()
yaml_output += kit.to_yaml(2)
yaml_output += patterns_to_yaml()

yaml_output
end

attr_reader :patterns, :tempo
attr_accessor :flow

Expand All @@ -127,36 +110,5 @@ def to_yaml(kit)
def longest_length_in_array(arr)
arr.inject(0) {|max_length, name| [name.to_s.length, max_length].max }
end

def flow_to_yaml
yaml_output = " Flow:\n"
ljust_amount = longest_length_in_array(@flow) + 1 # The +1 is for the trailing ":"
previous = nil
count = 0
@flow.each do |pattern_name|
if pattern_name == previous || previous.nil?
count += 1
else
yaml_output += " - #{(previous.to_s.capitalize + ':').ljust(ljust_amount)} x#{count}\n"
count = 1
end
previous = pattern_name
end
yaml_output += " - #{(previous.to_s.capitalize + ':').ljust(ljust_amount)} x#{count}\n"

yaml_output
end

def patterns_to_yaml
yaml_output = ""

# Sort to ensure a consistent order, to make testing easier
pattern_names = @patterns.keys.map {|key| key.to_s} # Ruby 1.8 can't sort symbols...
pattern_names.sort.each do |pattern_name|
yaml_output += "\n" + @patterns[pattern_name.to_sym].to_yaml()
end

yaml_output
end
end
end
16 changes: 0 additions & 16 deletions test/song_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,4 @@ def test_remove_unused_patterns
assert_equal(3, test_songs[:example_no_kit].patterns.length)
assert_equal(Hash, test_songs[:example_no_kit].patterns.class)
end

=begin
def test_to_yaml
test_songs = generate_test_data
kit_builder = KitBuilder.new("test/sounds")
kit_builder.add_item("bass", "bass_mono_8.wav")
kit_builder.add_item("snare", "snare_mono_8.wav")
kit_builder.add_item("hhclosed", "hh_closed_mono_8.wav")
kit_builder.add_item("hhopen", "hh_open_mono_8.wav")
kit = kit_builder.build_kit
result = test_songs[:example_with_kit].to_yaml(kit)
assert_equal(File.read("test/fixtures/yaml/song_yaml.txt"), result)
end
=end
end

0 comments on commit 691fffb

Please sign in to comment.