枢轴跟踪器是我使用的一个项目管理工具。我正在使用关键的跟踪器Ruby访问API。
我试图用每个成员分配给他们的点数来创建一个散列。
最终结果应该如下所示:
{"Joe Jones"=>5, "Jane Smith"=>6, "John Doe"=>3} 我已经在@members中为项目中的每个成员创建了一个数组。
我编写了下面的代码,以帮助我创建每个成员的散列和他/她分配的故事数量,但我真正需要的是分配给他们的每个故事的estimate值之和(估计值是给定故事的点值)。
#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因此,我想知道,对于每个成员,我如何能够总结出每个特定成员的estimate值。备注:未估计的故事在估计字段中有-1,不应包含在和中。
任何帮助,我可以修改上面的循环,以迭代每一个故事和和估计值(不包括负1s),将不胜感激。我有种感觉,有一些不错的可以完成这个任务!
project.stories.all(:owned_by => @members[i], :current_state => ['started', 'unstarted'])返回的示例数据,以防人们对格式感到好奇:
[#<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=[]>] 建议如何使这个标题更具描述性,将不胜感激。
发布于 2012-08-07 17:06:02
我想这就是你想要的:
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查看docs Enumerable#inject以获得详细信息
发布于 2012-08-07 17:35:16
只需在这里编码高尔夫,但这个片段可能也能做到这一点!
Hash[@members.collect {|member| Array(member, Project.storie_estimates_for(@member).inject(&:+))}]我将在storie_estimates_for模型中添加Project (成员)方法,该方法将返回一组估计值(当然是非负的!)为了这位会员的所有故事!
for循环被认为是邪恶的!循环播放
此外,注射是一种使用累加器将数组简化为单个对象的方法。如果您查看inject方法的形状:
ary.inject(initial) {|accumulator, element| do_something_with_accumulator_and_element }返回累加器的最终值。
https://stackoverflow.com/questions/11850319
复制相似问题