定期的なイベントの宝石ice_cube

一部のプロジェクトでは、定期的なイベントのルールをユーザーが構成できる必要があります。 イベントのルールは非常に複雑になる場合があります。たとえば、「月の最後から2番目の日ごと」や「特定の日付までの月の第2金曜日ごと」などです。 このような問題を解決するには、 gem ice_cubeを正常に使用できます。



Gem ice_cubeを使用すると、iCalendarに似たAPIを使用して定期的なイベントのルールを設定し、YAMLおよびRubyハッシュ形式でスケジュールをシリアル化/非シリアル化できます。 Ice_cubeを使用すると、1つのスケジュールのみを保存でき、以前に何百ものイベントをすぐに生成することはできません。 ice_cubeを選択した理由は、便利で柔軟なAPI、絶え間ない更新とバグ修正、他の開発者の間での人気です。



宝石のインストール:



gem install ice_cube
      
      





スケジュールを作成するには、IceCube :: Scheduleクラスを使用する必要があります。



 require 'rubygems' require 'ice_cube' include IceCube # : # - /   # { # :duration => 3600 -    # :end_time => Time.now + 3600 -   # } schedule = Schedule.new(Date.today) #   schedule.add_recurrence_time(Date.today) #   schedule.add_exception_time(Date.today + 1)
      
      





IceCube :: Ruleクラスは、繰り返しルールを作成するために使用されます。 Ice_ubeは、毎日、毎週、毎月、毎年、毎時、分、秒の繰り返しのルールを維持しています。 いくつかの使用例(より完全な例のリストはプロジェクトページにあります):



 #  4-  schedule.add_recurrence_rule Rule.daily(4) #   ,     schedule.add_recurrence_rule Rule.weekly(2).day(:monday, :friday) #  10, 20 ,      schedule.add_recurrence_rule Rule.monthly.day_of_month(10, 20, -1) #  ,       schedule.add_recurrence_rule Rule.monthly.day_of_week( :monday => [1], :tuesday => [-1] ) #  ,  50    100    schedule.add_recurrence_rule Rule.yearly.day_of_year(50, -100)
      
      





排他ルールを含むいくつかのルールを1つのスケジュールに組み合わせることができます。



 #  4-  /     schedule.add_recurrence_rule Rule.daily(4) schedule.add_exception_rule Rule.weekly.day(1, 5)
      
      





ルールの場合、特定の日付までの繰り返し回数に制限を設定できます。



 #  2- ,   schedule.add_recurrence_rule Rule.daily(2).count(10) #  2- ,    schedule.add_recurrence_rule Rule.daily(2).until(Date.today.next_month - Date.today.day)
      
      





さて、おそらく、最も興味深いのはスケジュール要求です:



 #    schedule.all_occurrences #       schedule.occurrences((Date.today + 5).to_time) #     schedule.occurs_at?(Time.now) #     schedule.occurs_on?(Date.today) #     schedule.occurs_between?(Time.now, (Date.today + 5).to_time) #    schedule.first schedule.first(3) #    schedule.next_occurrence #  3   schedule.next_occurrences(3) #    schedule.remaining_occurrences
      
      





YAML / HASH / iCal形式のデータのシリアル化:



 # YAML yaml = schedule.to_yaml Schedule.from_yaml(yaml) # Hash hash = schedule.to_hash Schedule.from_hash(hash) # iCalendar schedule.to_ical
      
      





gem ice_cubeを使用して、定期的なイベントのカレンダーをFullCalendarおよびDelayedJobと一緒に実装します 。使用例はこのページにあります



この記事のいくつかの例は、公式文書から取られています。 質問やコメントがあれば、喜んでお答えします。



参照:

github.com/seejohnrun/ice_cube-GitHubのプロジェクトページ。

seejohnrun.github.com/ice_cube/static/ice_cube_ruby_nyc.pdf-ice_cubeのプレゼンテーション。

seejohnrun.github.com/ice_cube/static/lsrc_ice_cube.pdfは別のプレゼンテーションです。

www.inexfinance.com/en/blog/2012/12/2/gem_ice_cube-この記事の英語版。

github.com/inex-finance/blog-examples-この記事の例。



All Articles