Skip to content

Commit

Permalink
minor cleanup. remove init args. use array fetch instead of []"
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Mar 12, 2015
1 parent 1770ef9 commit 7d15ee1
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/triad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class InvalidAddition < StandardError; end
class ItemNotPresent < StandardError; end

# stored as {key => ['Descriptor', value]}
def initialize(*args)
def initialize
@storage = ThreadSafe::Hash.new
end
attr_reader :storage
Expand Down Expand Up @@ -75,19 +75,16 @@ def value_exists?(value)
end

def with_interest(interest)
index = case
when key_exists?(interest)
0
when descriptor_exists?(interest)
1
when value_exists?(interest)
2
else
raise ItemNotPresent.new
end
position = case
when key_exists?(interest) then 0
when descriptor_exists?(interest) then 1
when value_exists?(interest) then 2
else
raise ItemNotPresent.new
end

storage.select{|key, array|
[key, *array][index] == interest
[key, *array].fetch(position) == interest
}.map{|key, array| [key, *array] }
end
end

0 comments on commit 7d15ee1

Please sign in to comment.