Class: VideoData::Video
- Inherits:
-
Object
- Object
- VideoData::Video
- Defined in:
- lib/video_data/video.rb
Overview
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The defined attributes from the csv file the video data was extacted.
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Hash-like access to the data * Args : -
attr
The attribute to be ouput. -
#celebrity ⇒ Object
Celebrity of the video.
-
#date ⇒ Object
Date of the video.
-
#initialize(attributes, data) ⇒ Video
constructor
From the attributes and the row line a VideoData::Video is created * Args : -
attributes
The attributes defined in the csv file. -
#method_missing(m, *args) ⇒ Object
Added dynamic predicates for valid tags.
-
#name ⇒ Object
Name of the video.
-
#tags ⇒ Object
The tags active for the video * Returns : -Returns an array of symbols.
-
#to_s ⇒ Object
Return a human friendly output with the video data.
Constructor Details
#initialize(attributes, data) ⇒ Video
From the attributes and the row line a VideoData::Video is created
-
Args :
-
attributes
The attributes defined in the csv file. The
order is vital to match attributes and values.
-
data
The row line that has all the video data in csv format
-
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 |
# File 'lib/video_data/video.rb', line 17 def initialize(attributes, data) @attributes = attributes @all_tags = attributes.dup [:videos, :date, :celebrity].each do |attr| @all_tags.delete attr end video_data = data.strip.split(',') @data = {} attributes.each_with_index do |attr, index| case video_data[index] when "x", "X" value = true when nil, "" value = attr.equal?(:celebrity) ? "" : false else value = video_data[index] end @data[attr] = value end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Added dynamic predicates for valid tags. eg if :eye_winged is a valid tag you can
video.eye_winged? # Outputs *true* or *false*
94 95 96 97 98 |
# File 'lib/video_data/video.rb', line 94 def method_missing(m, *args) # :nodoc: attr = m[0..-2].to_sym return @data[attr] if m =~ /(.*)\?/ and @all_tags.include?(attr) raise NoMethodError end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
The defined attributes from the csv file the video data was extacted.
10 11 12 |
# File 'lib/video_data/video.rb', line 10 def attributes @attributes end |
Instance Method Details
#[](attr) ⇒ Object
Hash-like access to the data
-
Args :
-
attr
The attribute to be ouput. Expects a symbol.
-
73 74 75 |
# File 'lib/video_data/video.rb', line 73 def [](attr) @data[attr] end |
#celebrity ⇒ Object
Celebrity of the video. Can be an empty string
54 55 56 |
# File 'lib/video_data/video.rb', line 54 def celebrity @data[:celebrity] end |
#date ⇒ Object
Date of the video
49 50 51 |
# File 'lib/video_data/video.rb', line 49 def date @data[:date] end |
#name ⇒ Object
Name of the video
44 45 46 |
# File 'lib/video_data/video.rb', line 44 def name @data[:videos] end |
#tags ⇒ Object
The tags active for the video
-
Returns : -Returns an array of symbols. Each symbol is an active tag in
the video
62 63 64 65 66 67 68 |
# File 'lib/video_data/video.rb', line 62 def = @data.dup [:videos, :date, :celebrity].each do |attr| .delete attr end .select { |key,value| value }.keys end |
#to_s ⇒ Object
Return a human friendly output with the video data.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/video_data/video.rb', line 78 def to_s = last_tag = .pop if .size > 1 = .join(', ') += " and #{last_tag}" if last_tag artist = celebrity.empty? ? "unknown" : celebrity "#{name} has a date of #{date} and his " + "celebrity is #{artist}, additionally this video" + " has this list of tags: #{}" end |