GithubHelp home page GithubHelp logo

ice_t's Introduction

IceT

Gem Version Code Climate Build Status Coverage Status

IceT is a ruby library for handling repeated events.

Installation

Add this line to your application's Gemfile:

gem 'ice_t'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ice_t

Usage

Creating rules

rule = IceT::Rule::Secondly.new(42)  # => every 42 seconds
rule = IceT::Rule::Minutely.new(42)  # => every 42 minutes
rule = IceT::Rule::Hourly.new(42)    # => every 42 hours
rule = IceT::Rule::Daily.new(42)     # => every 42 days
rule = IceT::Rule::Weekly.new(42)    # => every 42 weeks
rule = IceT::Rule::Monthly.new(42)   # => every 42 months
rule = IceT::Rule::Yearly.new(42)    # => every 42 years
rule = IceT::Rule::Secondly.new      # => every second

Rule in words (i18n)

# I18n.locale = :en
rule = IceT::Rule::Daily.new(14)
rule.to_s # => "every 14 days"

# I18n.locale = :de
rule = IceT::Rule::Daily.new(14)
rule.to_s # => "alle 14 Tage"

Get time occurrences

rule.occurrences(2.days.ago, Time.now) # => Array of times

IceT::Rule::Daily.new(14).occurrences(1.year.ago, Time.now).first(5)

Persistence

Store and restore the rule:

# JSON
rule = IceT::Rule::Daily.new(42)
json = rule.to_json
restored = IceT::Rule::Base.from_json(json)

# YAML
rule = IceT::Rule::Daily.new(42)
yaml = rule.to_yaml
restored = IceT::Rule::Base.from_yaml(yaml)

# Hash
rule = IceT::Rule::Daily.new(42)
hash = rule.to_hash
restored = IceT::Rule::Base.from_hash(hash)

Comparison and sorting

IceT::Rule::Daily.new(1) < IceT::Rule::Monthly.new(1) # => true
# compare different type of rules

minutely = IceT::Rule::Minutely.new(1)
hourly   = IceT::Rule::Hourly.new(1)
daily    = IceT::Rule::Daily.new(1)

hourly.between?(minutely, daily) # => true
minutely.between?(hourly, daily) # => false


# compare same type of rules

r1 = IceT::Rule::Daily.new(1)
r2 = IceT::Rule::Daily.new(2)
r3 = IceT::Rule::Daily.new(3)

r2.between?(r1, r2)	# => true
r1.between?(r2, r3)	# => false

# sort these rules

[r2,r1,r3].sort.collect(&:interval) # => [1, 2, 3]

License

This software is released under the MIT License.

Copyright © 2013-2014 Christian Nennemann (christian.nennemann[at]gmail[dot]com)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.