Вопрос

Pivotal Tracker is a project management tool that I use. I'm using the Pivotal Tracker Ruby Gem to access the API.

I'm trying to create a hash of each member with the number of points that they have assigned to them.

The end result should look something like:

{"Joe Jones"=>5, "Jane Smith"=>6, "John Doe"=>3} 

I've already created an array of each of the members in the project in @members.

I wrote the code below to help me create a hash of each member with the corresponding number of stories he/she has assigned, but what I really need is the sum of the estimate value for each story assigned to them (estimate is the point value for a given story).

#creates a hash of members to number of stories assigned.
    for i in 0..@members.length-1
      my_hash[@members[i]] = project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted']).length
    end

So, I'm wondering how, for each member, I can sum up the estimate value of each story owned_by that particular member. NOTE: Unestimated stories have -1 in the estimate field and should not be included in the sum.

Any help on how I can modify the above loop to iterate through each of the stories and sum the estimate values (excluding minus 1s) would be much appreciated. I have a feeling there's some nice Ruby jujitsu to get this done!

Sample data returned by project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted']) in case people are wondering about the format:

[#<PivotalTracker::Story:0x007f9d6b8 @id=314, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:23:42+00:00 ((2456097j,73422s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test ", @description="This is the description for \"Test\"", @story_type="feature", @estimate=5, @current_state="unstarted", @requested_by="joe jones", @owned_by="joe jones", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=315, @url="http://www.pivotaltracker.com/", @created_at=#<DateTime: 2012-06-18T20:25:20+00:00 ((2456097j,73520s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test 2", @description="This is the description for \"Test 2\"", @story_type="feature", @estimate=3, @current_state="unstarted", @requested_by="joe jones", @owned_by="joe jones"", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>, #<PivotalTracker::Story:0x007f9d6b8 @id=316, @url="http://www.pivotaltracker.com/story/", @created_at=#<DateTime: 2012-06-18T20:25:26+00:00 ((2456097j,73526s,0n),+0s,2299161j)>, @accepted_at=nil, @project_id=12345, @name="Test 3", @description="Description for Test 3 ", @story_type="feature", @estimate=-1, @current_state="started", @requested_by="joe jones", @owned_by="joe jones", @labels=nil, @jira_id=nil, @jira_url=nil, @other_id=nil, @integration_id=nil, @deadline=nil, @attachments=[]>] 

P.S. Suggestions for how to make this title more descriptive would be much appreciated.

Это было полезно?

Решение

I think here is what you are looking for:

for i in 0..@members.length-1
  my_hash[@members[i]] = project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted']).inject(0) do |sum, single_story|
    single_story.estimate > 0 ? sum + single_story.estimate : sum
  end
end

Have a look at the docs Enumerable#inject for details

Другие советы

Just code golfing here, but this snippet might do the trick as well!

Hash[@members.collect {|member| Array(member, Project.storie_estimates_for(@member).inject(&:+))}]

I would add the storie_estimates_for(member) method in the Project model that would return an array of estimates (non-negative of course!) for all stories for this member!

FWIW, the for loop is considered evil! http://blog.grayproductions.net/articles/the_evils_of_the_for_loop

Also, inject is a way to reduce an array to a single object using an accumulator. If you look at the shape of the inject method:

ary.inject(initial) {|accumulator, element| do_something_with_accumulator_and_element }

returns the final value of the accumulator.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top