Skip to content

Commit

Permalink
Handle mixed ids properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dropofwill committed May 15, 2015
1 parent 13c13cf commit 0c6e53c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
38 changes: 17 additions & 21 deletions lib/rtasklib/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ module Helpers
# make this module a stateless, singleton
extend self

# Filters should be a list of values
# Ranges interpreted as ids
# 1...5 : "1-5"
# 1..5 : "1-4"
# 1 : "1"
# and joined with ","
# [1...5, 8, 9] : "1-5,8,9"
#
# Converts ids, tags, and dom queries to a single string ready to pass
# directly to task.
#
Expand All @@ -30,7 +22,23 @@ def filter ids: nil, tags: nil, dom: nil
id_s = process_ids(ids) unless ids.nil?
tag_s = process_tags(tags) unless tags.nil?
dom_s = process_dom(dom) unless dom.nil?
return "#{id_s} #{tag_s} #{dom_s}"
return "#{id_s} #{tag_s} #{dom_s}".strip
end

# Filters should be a list of values
# Ranges interpreted as ids
# 1...5 : "1,2,3,4,5"
# 1..5 : "1,2,3,4"
# 1 : "1"
# and joined with ","
# [1...5, 8, 9] : "1,2,3,4,5,8,9"
#
# @api public
def id_a_to_s id_a
id_a.map do |el|
proc_ids = process_ids(el)
proc_ids
end.compact.join(",")
end

# Converts arbitrary id input to a task safe string
Expand All @@ -49,7 +57,6 @@ def process_ids ids
return ids
end
end
# private :process_ids

# Convert a range to a comma separated strings, e.g. 1..4 -> "1,2,3,4"
#
Expand All @@ -59,17 +66,6 @@ def process_ids ids
def id_range_to_s id_range
id_range.to_a.join(",")
end
# private :id_range_to_s

# @api public
def id_a_to_s id_a
id_a.map do |el|
proc_ids = process_ids(el)
proc_ids
end
.compact.join(",")
end
# private :id_range_to_s

# @api private
def process_tags tags
Expand Down
22 changes: 21 additions & 1 deletion spec/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe Rtasklib::Helpers do
puts Rtasklib::Helpers

describe 'Rtasklib::Helpers#to_gem_version' do

Expand All @@ -12,6 +11,7 @@
end

describe 'Rtasklib::Helpers#determine_type' do

it 'considers "10" an integer' do
expect(Rtasklib::Helpers.determine_type("10")).to eq(Integer)
end
Expand All @@ -37,4 +37,24 @@
expect(Rtasklib::Helpers.determine_type("on ")).to eq(String)
end
end

describe 'Rtasklib::Helpers#filter' do

it 'treats arrays of ranges properly' do
expect(Rtasklib::Helpers.filter(ids: [1..3,5...6])).to eq("1,2,3,5")
end

it 'treats arrays of strings properly' do
expect(Rtasklib::Helpers.filter(ids: ["1,2", "5"])).to eq("1,2,5")
end

it 'treats arrays of ints properly' do
expect(Rtasklib::Helpers.filter(ids: [1,2,3,4,5])).to eq("1,2,3,4,5")
end

it 'treats arrays mixed objects properly' do
expect(Rtasklib::Helpers.filter(ids: [1,2,3,4,5, 10..20, "7,8"]))
.to eq("1,2,3,4,5,10,11,12,13,14,15,16,17,18,19,20,7,8")
end
end
end

0 comments on commit 0c6e53c

Please sign in to comment.