Skip to content

Instantly share code, notes, and snippets.

@scicco
Created February 26, 2018 12:28
Show Gist options
  • Save scicco/5081a3b4599287ffcf26d92eb515eb82 to your computer and use it in GitHub Desktop.
Save scicco/5081a3b4599287ffcf26d92eb515eb82 to your computer and use it in GitHub Desktop.
test_result.rb
class Data
class << self
def all
(1..10).to_a
end
def created
[4, 5, 6, 8, 9]
end
def users
[1, 2, 4, 5, 6]
end
def accounts
[3, 7, 8, 9, 10]
end
def updated
[2, 3, 6, 7, 8, 9, 10]
end
end
end
class Activity < Data
def self.find(indexes: {})
result = []
if indexes.empty?
result << self.all
else
if indexes[:type].is_a?(Array)
if indexes[:type] != nil
if indexes[:type].include?(:accounts)
result += self.accounts
end
if indexes[:type].include?(:users)
result += self.users
end
end
else
if indexes[:type] == :accounts
result += self.accounts
end
if indexes[:type] == :users
result += self.users
end
end
if indexes[:action].is_a?(Array)
if indexes[:action] != nil
actions = []
if indexes[:action].include?(:created)
if result.empty?
result += self.created
else
actions += self.created
end
end
if indexes[:action].include?(:updated)
if result.empty?
result += self.updated
else
actions += self.updated
end
end
result = result & actions
end
else
if indexes[:action] == :created
if result.empty?
result += self.created
else
result = result & self.created
end
end
if indexes[:action] == :updated
if result.empty?
result += self.updated
else
result = result & self.updated
end
end
end
end
result = result.flatten
result = result.sort
result
end
end
if __FILE__ == $0
require 'minitest/autorun'
class ActivityTest < Minitest::Test
def test_one
assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], Activity.find
end
def test_two
assert_equal [4, 5, 6], Activity.find(:indexes => { :type => :users, :action => :created })
end
def test_three
assert_equal [2, 3, 6, 7, 8, 9, 10],
Activity.find(:indexes => { :type => [:accounts, :users], :action => :updated })
end
def test_four
assert_equal [2, 3, 4, 5, 6, 7, 8, 9, 10],
Activity.find(:indexes => { :type => [:accounts, :users], :action => [:updated, :created] })
end
def test_five
assert_equal [3, 7, 8, 9, 10],
Activity.find(:indexes => { :type => :accounts, :action => [:updated, :created] })
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment