Skip to content

Commit

Permalink
Remove duplication in has_one associations
Browse files Browse the repository at this point in the history
Seven `has_one` associations were defined in the same way, so the
code can be simplified by declaring `has_one` programmatically.

This commit does not change any functionality, with two exceptions:
"account.channel" and "video.rating" were the only two `has_one`
associations that would return +nil+ when the association was not
found, rather than raising an Error. Now they raise an error too,
in coherence with the other associations.
  • Loading branch information
claudiob committed Jun 6, 2014
1 parent 3bdbbff commit e713006
Show file tree
Hide file tree
Showing 29 changed files with 193 additions and 409 deletions.
20 changes: 19 additions & 1 deletion lib/yt/associations.rb
Expand Up @@ -33,6 +33,24 @@ def has_many(attributes, options = {})
delegate *options[:delegate], to: attributes if options[:delegate]
end

alias has_one has_many

def has_one(attribute, options = {})
delegate *options[:delegate], to: attribute if options[:delegate]

attributes = attribute.to_s.pluralize
require "yt/collections/#{attributes}"
mod = attributes.sub(/.*\./, '').camelize
collection = "Yt::Collections::#{mod.pluralize}".constantize

define_method attribute do
ivar = instance_variable_get "@#{attribute}"
instance_variable_set "@#{attribute}", ivar || send(attributes).first!
end

define_method attributes do
ivar = instance_variable_get "@#{attributes}"
instance_variable_set "@#{attributes}", ivar || collection.of(self)
end
end
end
end
20 changes: 0 additions & 20 deletions lib/yt/associations/channels.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/yt/associations/details_sets.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/yt/associations/ids.rb

This file was deleted.

39 changes: 0 additions & 39 deletions lib/yt/associations/ratings.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/yt/associations/snippets.rb

This file was deleted.

20 changes: 0 additions & 20 deletions lib/yt/associations/statuses.rb

This file was deleted.

21 changes: 0 additions & 21 deletions lib/yt/associations/user_infos.rb

This file was deleted.

19 changes: 19 additions & 0 deletions lib/yt/models/video.rb
Expand Up @@ -6,6 +6,25 @@ class Video < Resource
has_one :details_set, delegate: [:duration]
has_one :rating
has_many :annotations

def liked?
rating.rating == :like
end

def like
rating.update :like
liked?
end

def dislike
rating.update :dislike
!liked?
end

def unlike
rating.update :none
!liked?
end
end
end
end
7 changes: 7 additions & 0 deletions spec/associations/device_auth/account_spec.rb
@@ -0,0 +1,7 @@
require 'spec_helper'
require 'yt/models/account'

describe Yt::Account, :device_app do
it { expect($account.channel).to be_a Yt::Channel }
it { expect($account.user_info).to be_a Yt::UserInfo }
end
20 changes: 20 additions & 0 deletions spec/associations/device_auth/channel_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'
require 'yt/models/channel'

describe Yt::Channel, :device_app do
let(:channel) { Yt::Channel.new id: id, auth: $account }

context 'given an existing channel' do
let(:id) { 'UCxO1tY8h1AhOz0T4ENwmpow' }

it { expect(channel.snippet).to be_a Yt::Snippet }
it { expect(channel.status).to be_a Yt::Status }
end

context 'given an unknown channel' do
let(:id) { 'not-a-channel-id' }

it { expect{channel.snippet}.to raise_error Yt::Errors::NoItems }
it { expect{channel.status}.to raise_error Yt::Errors::NoItems }
end
end
8 changes: 0 additions & 8 deletions spec/associations/device_auth/channels_spec.rb

This file was deleted.

18 changes: 0 additions & 18 deletions spec/associations/device_auth/details_sets_spec.rb

This file was deleted.

20 changes: 20 additions & 0 deletions spec/associations/device_auth/playlist_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'
require 'yt/models/playlist'

describe Yt::Playlist, :device_app do
let(:playlist) { Yt::Playlist.new id: id, auth: $account }

context 'given an existing playlist' do
let(:id) { 'PLSWYkYzOrPMRCK6j0UgryI8E0NHhoVdRc' }

it { expect(playlist.snippet).to be_a Yt::Snippet }
it { expect(playlist.status).to be_a Yt::Status }
end

context 'given an unknown playlist' do
let(:id) { 'not-a-playlist-id' }

it { expect{playlist.snippet}.to raise_error Yt::Errors::NoItems }
it { expect{playlist.status}.to raise_error Yt::Errors::NoItems }
end
end
28 changes: 0 additions & 28 deletions spec/associations/device_auth/ratings_spec.rb

This file was deleted.

@@ -1,7 +1,7 @@
require 'spec_helper'
require 'yt/associations/ids'
require 'yt/models/resource'

describe Yt::Associations::Ids, :device_app do
describe Yt::Resource, :device_app do
subject(:resource) { Yt::Resource.new url: url, auth: $account }

describe '#id' do
Expand Down
39 changes: 0 additions & 39 deletions spec/associations/device_auth/snippets_spec.rb

This file was deleted.

0 comments on commit e713006

Please sign in to comment.