2017-08-30 245 views
0

我一直在使用Enum.reduce增加日期

Enum.reduce(email_array, {[], [], []}, fn email, {shares, share_requests, changes} = _acc -> 

end) 

我會在每個減速器傳遞方法

CameraActivity.log_activity(caller, camera, "shared", %{with: email, ip: requester_ip}, done_at) 

在done_at,默認值是Ecto.DateTime.utc,但我想在每個Enum.reduce上增加這個日期和時間。

我試圖在Enum.reduce的最開始處聲明datetime爲done_at = Ecto.DateTime.utc,但是我不知道如何在Enum.reduce中循環它,以便我可以增加幾秒鐘進入它。這隻會改變每次迭代的活動時間。任何幫助將不勝感激感謝

更新Dogbert

Enum.reduce(email_array, {[], [], []}, fn email, {shares, share_requests, changes} = _acc -> 
    with {:found_user, sharee} <- ensure_user(email) 
    do 
    case CameraShare.create_share(camera, sharee, caller, params["rights"], params["message"]) do 
     {:ok, camera_share} -> 
     unless caller == sharee do 
      send_email_notification(caller, camera, sharee.email, camera_share.message) 
     end 
     Camera.invalidate_user(sharee) 
     Camera.invalidate_camera(camera) 
     CameraActivity.log_activity(caller, camera, "shared", %{with: sharee.email, ip: requester_ip}) 
     {[camera_share | shares], share_requests, changes} 
     {:error, changeset} -> 
     {shares, share_requests, [attach_email_to_message(changeset, email) | changes]} 
    end 
    else 
    {:not_found, email} -> 
     case CameraShareRequest.create_share_request(camera, email, caller, params["rights"], params["message"]) do 
     {:ok, camera_share_request} -> 
      send_email_notification(caller, camera, email, camera_share_request.message, camera_share_request.key) 
      CameraActivity.log_activity(caller, camera, "shared", %{with: email, ip: requester_ip}) 
      Intercom.intercom_activity(Application.get_env(:evercam_media, :create_intercom_user), get_user_model(email), get_user_agent(conn), requester_ip, "Shared-Non-Registered") 
      {shares, [camera_share_request | share_requests], changes} 
     {:error, changeset} -> 
      {shares, share_requests, [attach_email_to_message(changeset, email) | changes]} 
     end 
    end 
end) 
+0

你爲什麼不保存'done_at'的電流值在累加器以及在每次迭代增量呢? – Dogbert

+0

我只擔心增量。怎麼做? –

+0

也不知道如何將它保存在累加器中,實際上累加器也是從您以前的問題中學習到的 –

回答

1

你已經有3元組作爲Enum.reduce/3呼叫累加器。您可以將日期時間值添加爲元組中的第4項,並在每次迭代中增加它。例如,通過5秒增加它,你可以這樣做:

Enum.reduce(email_array, {[], [], [], Ecto.DateTime.utc}, fn email, {shares, share_requests, changes, datetime} = _acc -> 
    # next_datetime is datetime + 5 seconds 
    next_datetime = datetime |> 
    Ecto.DateTime.to_erl |> 
    :calendar.datetime_to_gregorian_seconds |> 
    Kernel.+(5) |> 
    :calendar.gregorian_seconds_to_datetime |> 
    Ecto.DateTime.from_erl 

    # `next_datetime` should be returned as the 4th value of each tuple below. 
    # `datetime` can be used as the current value. 

    with {:found_user, sharee} <- ensure_user(email) 
    do 
    case CameraShare.create_share(camera, sharee, caller, params["rights"], params["message"]) do 
     {:ok, camera_share} -> 
     ... 
     {[camera_share | shares], share_requests, changes, next_datetime} 
     {:error, changeset} -> 
     {shares, share_requests, [attach_email_to_message(changeset, email) | changes], next_datetime} 
    end 
    else 
    {:not_found, email} -> 
     case CameraShareRequest.create_share_request(camera, email, caller, params["rights"], params["message"]) do 
     {:ok, camera_share_request} -> 
      ... 
      {shares, [camera_share_request | share_requests], changes, next_datetime} 
     {:error, changeset} -> 
      {shares, share_requests, [attach_email_to_message(changeset, email) | changes], next_datetime} 
     end 
    end 
end)