Skip to content

Instantly share code, notes, and snippets.

@perigrin
Created June 8, 2009 20:27
Show Gist options
  • Save perigrin/126041 to your computer and use it in GitHub Desktop.
Save perigrin/126041 to your computer and use it in GitHub Desktop.
# class RecommendationTracker
# def add_recommendation(recommender, business)
# has_recommender_recommended_business?(recommender, business) do
# raise StandardError, 'You may only recommend the same business once.'
# end
#
# @recommendations[business] << recommender
# end
#
# def recommendation_totals
# recommendation_totals = Hash.new
#
# @recommendations.each_key do |business|
# recommendation_totals[business] = @recommendations[business].length
# end
#
# recommendation_totals
# end
#
# private
# def initialize
# @recommendations = Hash.new do |recommendations, business|
# recommendations[business] = Array.new
# end
# end
#
# def has_recommender_recommended_business?(recommender, business)
# return false if [email protected]_key? business
# return false if !@recommendations[business].include?(recommender)
#
# yield
# true
# end
# end
use 5.10.0;
use MooseX::Declare;
use MooseX::AttributeHelpers;
class ReccomendationTracker {
has recommended_business => (
isa => 'HashRef[ArrayRef]',
is => 'ro',
default => sub { {} },
);
method add_recommendation( $reccomender, $business ) {
confess 'You may only recommend the same business once.'
if $self->recommended_business->{$business} ~~ $reccomender;
$self->recommended_business->{$business} //= [];
push $self->recommended_business->{$business}, $reccomender;
};
method recommendation_totals() {
return {
map { $_ => scalar @{$_} }
keys %{ $self->recommended_business }
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment