Skip to content

Commit

Permalink
improve ordering/sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Nov 24, 2014
1 parent 73e2ea4 commit 06adca5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 40 deletions.
7 changes: 6 additions & 1 deletion lib/tivohmo/adapters/plex/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ def initialize(identifier)
def children
synchronize do
if super.blank?
Array(server.library.sections).each do |section|
sections = Array(server.library.sections)
# Sort by title descending so that creation times are
# correct for tivo sort of newest first (Time.now for
# created_at in Section)
sections = sections.sort_by{|s| s[:title] }.reverse
sections.each do |section|
add_child(Section.new(section))
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tivohmo/adapters/plex/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(delegate, category_type, category_value=nil)
end

self.modified_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.now
end

def children
Expand Down
9 changes: 7 additions & 2 deletions lib/tivohmo/adapters/plex/qualified_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(delegate, category_type, category_qualifier)
self.category_qualifier = category_qualifier
self.title = category_type.to_s.titleize
self.modified_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.now
end

def children
Expand All @@ -35,7 +35,12 @@ def children
end

if super.blank?
Array(delegate.send(category_qualifier)).each do |category_value|
qualified = Array(delegate.send(category_qualifier))
# Sort by title descending so that creation times are
# correct for tivo sort of newest first (Time.now for
# created_at in Category)
qualified = qualified.sort_by{|c| c[:title] }.reverse
qualified.each do |category_value|
add_child(Category.new(delegate, category_type, category_value))
end
end
Expand Down
22 changes: 13 additions & 9 deletions lib/tivohmo/adapters/plex/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,28 @@ def initialize(delegate)

self.title = delegate.title
self.modified_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.at(delegate.updated_at.to_i)
self.created_at = Time.now
end

def children
synchronize do
if super.blank?
add_child(Category.new(delegate, :recently_added))
# Tivo time sorting is reverse chronological (newest first), so
# order it here in reverse order so the creation time cause the
# right sorting ("all" is newest and comes first)
add_child(QualifiedCategory.new(delegate, :by_collection, :collections))
add_child(QualifiedCategory.new(delegate, :by_content_rating, :content_ratings))
add_child(QualifiedCategory.new(delegate, :by_folder, :folders))
add_child(QualifiedCategory.new(delegate, :by_genre, :genres))
add_child(QualifiedCategory.new(delegate, :by_year, :years))
add_child(QualifiedCategory.new(delegate, :by_first_character, :first_characters))

#add_child(Category.new(delegate, :unwatched))
add_child(Category.new(delegate, :newest))
add_child(Category.new(delegate, :on_deck))
add_child(Category.new(delegate, :newest))
add_child(Category.new(delegate, :recently_viewed))
add_child(Category.new(delegate, :recently_added))
add_child(Category.new(delegate, :all))
add_child(QualifiedCategory.new(delegate, :by_genre, :genres))
add_child(QualifiedCategory.new(delegate, :by_year, :years))
add_child(QualifiedCategory.new(delegate, :by_first_character, :first_characters))
add_child(QualifiedCategory.new(delegate, :by_collection, :collections))
add_child(QualifiedCategory.new(delegate, :by_folder, :folders))
add_child(QualifiedCategory.new(delegate, :by_content_rating, :content_ratings))
end
end

Expand Down
22 changes: 13 additions & 9 deletions spec/adapters/plex/category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
describe "#initialize" do

it "should instantiate" do
category = described_class.new(plex_delegate, :newest)
expect(category).to be_a described_class
expect(category).to be_a TivoHMO::API::Container
expect(category.category_type).to eq(:newest)
expect(category.category_value).to be_nil
expect(category.title).to eq('Newest')
expect(category.identifier).to eq(plex_delegate.key)
expect(category.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(category.created_at).to eq(Time.at(plex_delegate.added_at))
now = Time.now
Timecop.freeze(now) do
category = described_class.new(plex_delegate, :newest)
expect(category).to be_a described_class
expect(category).to be_a TivoHMO::API::Container
expect(category.category_type).to eq(:newest)
expect(category.category_value).to be_nil
expect(category.title).to eq('Newest')
expect(category.identifier).to eq(plex_delegate.key)
expect(category.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(category.created_at).to eq(now)
end

end

it "should use_category_value for title if present" do
Expand Down
25 changes: 15 additions & 10 deletions spec/adapters/plex/qualified_category_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
describe "#initialize" do

it "should instantiate" do
qcat = described_class.new(plex_delegate, :by_year, '2000')
expect(qcat).to be_a described_class
expect(qcat).to be_a TivoHMO::API::Container
expect(qcat.title).to eq("By Year")
expect(qcat.category_qualifier).to eq('2000')
expect(qcat.title).to eq("By Year")
expect(qcat.identifier).to eq(plex_delegate.key)
expect(qcat.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(qcat.created_at).to eq(Time.at(plex_delegate.added_at))
now = Time.now
Timecop.freeze(now) do
qcat = described_class.new(plex_delegate, :by_year, '2000')
expect(qcat).to be_a described_class
expect(qcat).to be_a TivoHMO::API::Container
expect(qcat.title).to eq("By Year")
expect(qcat.category_qualifier).to eq('2000')
expect(qcat.title).to eq("By Year")
expect(qcat.identifier).to eq(plex_delegate.key)
expect(qcat.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(qcat.created_at).to eq(now)
end
end

end
Expand All @@ -43,7 +46,9 @@
expect(section.children.size).to eq(2)
expect(section.children[0]).to be_instance_of(TivoHMO::Adapters::Plex::Category)
expect(section.children[0].category_type).to be(:by_year)
expect(section.children[0].category_value).to eq(listing[0])
# reverse order due to sorting by title reversed to work with tivo newest first sorting
expect(section.children[0].category_value).to eq(listing[1])
expect(section.children[1].category_value).to eq(listing[0])
end

end
Expand Down
19 changes: 11 additions & 8 deletions spec/adapters/plex/section_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
describe "#initialize" do

it "should instantiate" do
section = described_class.new(plex_delegate)
expect(section).to be_a described_class
expect(section).to be_a TivoHMO::API::Container
expect(section.title).to eq(plex_delegate.title)
expect(section.identifier).to eq(plex_delegate.key)
expect(section.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(section.created_at).to eq(Time.at(plex_delegate.added_at))
now = Time.now
Timecop.freeze(now) do
section = described_class.new(plex_delegate)
expect(section).to be_a described_class
expect(section).to be_a TivoHMO::API::Container
expect(section.title).to eq(plex_delegate.title)
expect(section.identifier).to eq(plex_delegate.key)
expect(section.modified_at).to eq(Time.at(plex_delegate.updated_at))
expect(section.created_at).to eq(now)
end
end

end
Expand All @@ -30,7 +33,7 @@
section = described_class.new(plex_delegate)
expect(section.children.size).to eq(11)
classes = [TivoHMO::Adapters::Plex::Category, TivoHMO::Adapters::Plex::QualifiedCategory]
expect(section.children.collect(&:class).uniq).to eq(classes)
expect(section.children.collect(&:class).uniq.sort_by(&:name)).to eq(classes.sort_by(&:name))
end

end
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
RSpec.configure do |config|
end

require 'timecop'

require 'coveralls'
Coveralls.wear!

Expand Down
1 change: 1 addition & 0 deletions tivohmo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec"
spec.add_development_dependency "rack-test"
spec.add_development_dependency "nokogiri"
spec.add_development_dependency "timecop"

# core dependencies
spec.add_dependency "activesupport"
Expand Down

0 comments on commit 06adca5

Please sign in to comment.