kivanio (owner)

Fork Of

gist: 217641 by rklemme Complete file for blog entr...

Revisions

gist: 218353 Download_button fork
public
Public Clone URL: git://gist.github.com/218353.git
Embed All Files: show embed
album.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'yaml'
 
Track = Struct.new :title, :duration
 
# An Album represents an audio medium which has title,
# interpret, a pause duration between tracks and a list
# of individual tracks.
class Album
  attr_reader :title, :interpret, :pause
 
  # initialization
 
  def initialize(title, interpret, pause = 0)
    super()
 
    # main properties
    self.title = title
    self.interpret = interpret
    @tracks = []
 
    # additional properties
    self.pause = pause
 
    # redundant properties
    # left out
  end
 
  def title=(t)
    @title = t.dup.freeze
  end
 
  def interpret=(i)
    @interpret = i.dup.freeze
  end
 
  def pause=(time)
    @pause = time
    @duration = nil
  end
 
  def each_track(&b)
    @tracks.each(&b)
    self
  end
 
  def add_track(tr)
    @tracks << tr
    @duration = nil
    self
  end
 
  def clear_tracks
    @tracks.clear
    @duration = nil
    self
  end
 
  alias << add_track
 
  def tracks
    @tracks.dup
  end
 
  def duration
    @duration ||= @tracks.empty? ?
      0 :
      @tracks.inject(-pause) do |sum, tr|
        sum + pause + tr.duration
      end
  end
 
  # conversion to a printable string
 
  def to_s
    "Album '#{title}' by '#{interpret}' (#{tracks.size} tracks)"
  end
 
  # equivalence
 
  def eql?(album)
    self.class.equal?(album.class) &&
      title == album.title &&
      interpret == album.interpret &&
      tracks == album.tracks
  end
 
  alias == eql?
 
  # hash code calculation
 
  def hash
    title.hash ^
      interpret.hash ^
      tracks.hash
  end
 
  # comparability
 
  include Comparable
 
  def <=>(o)
    c = interpret <=> o.interpret
    return c unless c == 0
    c = title <=> o.title
    return c unless c == 0
    c = tracks <=> o.tracks
    return c unless c == 0
    pause <=> o.pause
  end
 
  # cloning
 
  def initialize_copy(source)
    super
    @tracks = @tracks.dup
  end
 
  # freezing
 
  def freeze
    # unless frozen?
      @tracks.freeze
      duration
    # end
    super
  end
 
  # customized persistence
 
  def marshal_dump
    a = (super rescue [])
    a.push(@title, @interpret, @pause, @tracks)
  end
 
  def marshal_load(dumped)
    super rescue nil
    @title, @interpret, @pause, @tracks = *dumped.shift(4)
  end
 
  def to_yaml_properties
    a = super
    a.delete :@duration
    a
  end
 
  # matching
 
  def ===(track)
    t = (track.title rescue track)
    tracks.find {|tr| t == tr.title}
  end
 
  def =~(rx)
    rx =~ title
  end
 
end
 
# ===== Test Code ===================================================
 
alb = Album.new "In through the out door", "Led Zeppelin", 2
printf "%-30s %p\n", 'empty album', alb
 
alb << Track.new("In the Evening", 6 * 60 + 49)
alb << Track.new("South Bound Saurez", 4 * 60 + 12)
alb << Track.new("Fool in the Rain", 6 * 60 + 12)
alb << Track.new("Hot Dog", 3 * 60 + 17)
 
printf "%-30s %p\n", 'page 1 completed', alb
printf "%-30s %p\n", 'duration', alb.duration
printf "%-30s %p\n", 'after duration calculation', alb
 
page1 = alb.dup
printf "%-30s %p\n", 'copy of page 1', page1
printf "%-30s %p\n", 'copy equivalent?', alb == page1
printf "%-30s %p\n", 'really?', page1 == alb
 
alb << Track.new("Carouselambra", 10 * 60 + 32)
alb << Track.new("All my Love", 5 * 60 + 51)
alb << Track.new("I'm gonna crawl", 5 * 60 + 30)
 
alb.duration
 
printf "%-30s %p\n", 'full album', alb
printf "%-30s %p\n", 'page 1', page1
 
printf "%-30s %p\n", 'track found?', alb === "All my Love"
printf "%-30s %p\n", 'track on page 1', page1 === "All my Love"
 
puts alb, page1
 
2.times { alb.freeze }
 
printf "%-30s %p\n", 'full album', alb
 
alb2 = Marshal.load(Marshal.dump(alb))
printf "%-30s %p\n", 'marshaled', alb2
 
alb3 = YAML.load(YAML.dump(alb))
printf "%-30s %p\n", 'yamld', alb3
printf "%-30s %p\n", 'yamld duration', alb3.duration