Class: VideoData::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/video_data/video.rb

Overview

Class that represents the data of a video. Has dynamic predicates for valid tags. If :eye_winged is a valid tag you can

video = VideoData.new(attributes, data)
video.eye_winged?  # Outputs *true* or *false*

Instance Attribute Summary collapse

Instance Method Summary collapse

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*

Raises:

  • (NoMethodError)


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

#attributesObject (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

#celebrityObject

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

#dateObject

Date of the video



49
50
51
# File 'lib/video_data/video.rb', line 49

def date
  @data[:date]
end

#nameObject

Name of the video



44
45
46
# File 'lib/video_data/video.rb', line 44

def name
  @data[:videos]
end

#tagsObject

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 tags
  tags = @data.dup
  [:videos, :date, :celebrity].each do |attr|
    tags.delete attr
  end
  tags.select { |key,value| value }.keys
end

#to_sObject

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
  video_tags = tags
  last_tag = video_tags.pop if video_tags.size > 1
  output_tags = video_tags.join(', ')
  output_tags += " 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: #{output_tags}"
end