Class: Diane::Player
- Inherits:
-
Object
- Object
- Diane::Player
- Defined in:
- lib/diane/player.rb
Overview
Reconstructs and plays back recordings
Instance Attribute Summary collapse
-
#recordings ⇒ Object
readonly
Returns the value of attribute recordings.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#all_recordings ⇒ Object
Returns hash array of all recordings in DIANE file.
-
#initialize(opts) ⇒ Player
constructor
A new instance of Player.
-
#play ⇒ Object
returns and puts formatted recordings returned by query.
-
#preface ⇒ Object
generates shell message describing the recordings returned by query.
-
#query(recordings) ⇒ Object
Generates a subset of recordings using command options (number, user, order).
Constructor Details
#initialize(opts) ⇒ Player
Returns a new instance of Player.
9 10 11 12 13 14 15 16 |
# File 'lib/diane/player.rb', line 9 def initialize(opts) @num = opts.fetch(:num, 1) @inorder = opts.fetch(:inorder, false) @user = opts.fetch(:user, USER) @everyone = opts.fetch(:everyone, false) @all = opts.fetch(:all, false) @recordings = query(all_recordings) end |
Instance Attribute Details
#recordings ⇒ Object (readonly)
Returns the value of attribute recordings.
7 8 9 |
# File 'lib/diane/player.rb', line 7 def recordings @recordings end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
7 8 9 |
# File 'lib/diane/player.rb', line 7 def user @user end |
Instance Method Details
#all_recordings ⇒ Object
Returns hash array of all recordings in DIANE file
20 21 22 23 24 25 26 27 |
# File 'lib/diane/player.rb', line 20 def all_recordings opts = { headers: true, header_converters: :symbol, encoding: 'utf-8' } CSV.read(DIFILE, opts).map(&:to_hash) end |
#play ⇒ Object
returns and puts formatted recordings returned by query
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/diane/player.rb', line 55 def play abort %(None from #{@user}. Fuck off.).magenta if @recordings.empty? stdout = preface @recordings.each do |r| stdout += "\n#{r[:time]} : ".cyan + "@#{r[:user]}".yellow stdout += "\n#{r[:message]}\n\n" end puts stdout stdout end |
#preface ⇒ Object
generates shell message describing the recordings returned by query
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/diane/player.rb', line 41 def preface position = @inorder ? 'first' : 'last' scope = @user == USER ? 'your' : "#{@user}'s" preface = if @recordings.length == 1 %(Here's the #{position} of #{scope} recordings:\n) else %(Here's the #{position} #{@recordings.length} of #{scope} recordings:\n) end preface.green end |
#query(recordings) ⇒ Object
Generates a subset of recordings using command options (number, user, order)
31 32 33 34 35 36 37 |
# File 'lib/diane/player.rb', line 31 def query(recordings) @num += 1 if @num.zero? recordings.select! { |r| r[:user] == @user } unless @everyone limit = @all ? recordings.length : [@num, recordings.length].min recordings.reverse! unless @inorder recordings.take(limit) end |