Class: Itunes::Track

Inherits:
Object
  • Object
show all
Extended by:
Util::Executor
Includes:
Util::Executor
Defined in:
lib/itunes/track.rb

Constant Summary collapse

ATTRIBUTES =
[
  :persistent_id,
  :name,
  :album,
  :artist,
  :track_count,
  :track_number,
  :year,
  :season_number,
  :episode_number,
  :show,
  :video_kind
].freeze
FINDER_ATTRIBUTES =
[
  :persistent_id,
  :name,
  :album,
  :artist
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Executor

execute_script, execute_template_based_script, generate_script_from_template, script_base_dir, script_tmp_dir

Constructor Details

#initialize(args = nil) ⇒ Track

Returns a new instance of Track.



33
34
35
36
37
38
39
# File 'lib/itunes/track.rb', line 33

def initialize(args=nil)
  if args.is_a?(Hash)
    args.each do |attr, val|
      send("#{attr}=", val) if ATTRIBUTES.include?(attr)
    end
  end
end

Class Method Details

.allObject



106
107
108
# File 'lib/itunes/track.rb', line 106

def self.all
  find_by(:all)
end

.find_by(arg) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/itunes/track.rb', line 82

def self.find_by(arg)
  raise ArgumentError.new('nil argument is given') if arg.nil?

  if arg.is_a?(Hash)
    conditions = find_conditions(arg)
    whose = 'whose'
  elsif arg == :all
    conditions = ''
    whose = ''
  else
    raise ArgumentError.new('unsupported rgument is given')
  end

  script_name = generate_script_from_template('track/finder.tmpl.scpt', whose: whose, conditions: conditions)
  track_response = execute_template_based_script(script_name)

  tracks_attributes = ::JSON.parse(track_response)
  tracks_attributes.compact.map do |track_attributes|
    Track.new.tap do |track|
      track.assign_attributes_by(track_attributes)
    end
  end
end

Instance Method Details

#assign_attributes_by(track_attributes) ⇒ Object



78
79
80
# File 'lib/itunes/track.rb', line 78

def assign_attributes_by(track_attributes)
  ATTRIBUTES.each { |attr| send("#{attr}=", track_attributes[attr.to_s]) }
end

#convertObject



41
42
43
44
# File 'lib/itunes/track.rb', line 41

def convert
  converted_persistent_id = execute_script('track/convert.scpt', self.persistent_id)
  Track.find_by(persistent_id: converted_persistent_id).first
end

#delete!Object



46
47
48
49
# File 'lib/itunes/track.rb', line 46

def delete!
  execute_script('track/delete.scpt', self.persistent_id)
  nil
end

#pauseObject



56
57
58
59
# File 'lib/itunes/track.rb', line 56

def pause
  Itunes::Player.pause
  self
end

#playObject



51
52
53
54
# File 'lib/itunes/track.rb', line 51

def play
  execute_script('track/play.scpt', self.persistent_id)
  self
end

#stopObject



61
62
63
64
# File 'lib/itunes/track.rb', line 61

def stop
  Itunes::Player.stop
  self
end

#update(attributes) ⇒ Object Also known as: update_attributes

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
# File 'lib/itunes/track.rb', line 66

def update(attributes)
  raise ArgumentError.new('Invalid argument is given') unless attributes.is_a?(Hash)

  records = update_attribute_records(attributes)
  update_targets = { persistent_id: self.persistent_id, update_records: records }
  script_name = generate_script_from_template('track/updater.tmpl.scpt', update_targets)
  execute_template_based_script(script_name)
  attributes.each { |key, val| send("#{key}=", val) }
  self
end