Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
In Ruby
require 'minitest/autorun'
require 'minitest/pride'
# Will return a single code and code set if one exists in the code sets that are
# passed in. Returns a hash with a key of code and code_set if found, nil otherwise
def preferred_code(preferred_code_sets, codes_attribute=:codes, value_set_map=nil)
codes_value = send(codes_attribute)
preferred_code_sets = value_set_map ? (preferred_code_sets & value_set_map.collect{|cs| cs["set"]}) : preferred_code_sets
matching_code_sets = preferred_code_sets & codes_value.keys
#if matching_code_sets.present? <= present is only used in rails empty? is the ruby version of present?
unless matching_code_sets.empty?
if value_set_map
matching_code_sets.each do |matching_code_set|
matching_codes = codes_value[matching_code_set] & value_set_map.collect{|cs| cs["set"] == matching_code_set ? cs["values"] : []}.flatten.compact
if !matching_codes.empty?
return {'code' => matching_codes.first, 'code_set' => matching_code_set}
end
end
else
code_set = matching_code_sets.first
{'code' => codes_value[code_set].first, 'code_set' => code_set}
end
else
nil
end
end
describe 'TestMethods' do
describe 'preferred_code' do
def codes;{}; end
it "#returns nil" do
preferred_code([]).must_be_nil
end
describe 'the else scenario' do
def codes;{'code' => 0, 'code_set' => 0}; end
it "value_set_map is nil" do
preferred_code(['set','code', 'code_set','done']).must_equal({'code' => 1, 'code_set' => 1})
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment