Giter Site home page Giter Site logo

ice_cube_model's Introduction

Gem Version Build Status Code Climate Test Coverage

ice_cube_model

Extend any "cron-expression" object with ice_cube (calendar repeating event) capabilities.

Add ice_cube methods to a class (e.g. active_record, active_model) that has cron expression fields.

description

ice_cube is a sold library for projecting and querying recurrence rules. Cron is the standard for expressing recurrence rules. If you have a model that stores cron fields in separate attributes, then this gem will allow you to map those attributes into ice_cube and execute projection and queries against it.

The fields in the model can use standard cron expression syntax explained here. This includes range expressions, series expressions, "last" day of month, Nth weekday of month, etc.

PLEASE NOTE: This gem is a work-in-progress. Many features have yet to be implemented.

installation

gem install ice_cube_model

or add to Gemfile

gem "ice_cube_model"

example

class Appointment
    attr_accessor 
        :repeat_start_date,
        :repeat_interval,
        :repeat_year,
        :repeat_month,
        :repeat_day,
        :repeat_weekday

    include ::IceCubeModel::Base
end

appointment = Appointment.new
appointment.repeat_start_date = ::Date.new(2015, 3, 5)
appointment.repeat_day = 5

appointment.occurrences_between(::Date.new(2015, 3, 5), ::Date.new(2015, 6, 5))
# => [2015-03-05, 2015-04-05, 2015-06-05]

remap attributes

If needed, you can remap the attributes expected by ice_cube to other attributes or methods on the class.

# map to another attribute
class Appointment
    attr_accessor 
        :start_date,
        :repeat_interval,
        :repeat_year,
        :repeat_month,
        :repeat_day,
        :repeat_weekday

    include ::IceCubeModel::Base

    with_repeat_param :repeat_start_date, :start_date
end

# map to a method
class Appointment
    attr_accessor 
        :repeat_interval,
        :repeat_year,
        :repeat_month,
        :repeat_day,
        :repeat_weekday

    include ::IceCubeModel::Base

    with_repeat_param :repeat_start_date, :calculate_start_date

    def calculate_start_date
        Date.current + 7.days
    end
end

# map to a lambda
class Appointment
    attr_accessor 
        :repeat_interval,
        :repeat_year,
        :repeat_month,
        :repeat_day,
        :repeat_weekday

    include ::IceCubeModel::Base

    with_repeat_param :repeat_start_date, lambda { Date.current + 7.days }
end

recurrence rule examples (date)

desc interval year month day weekday
1st of every month 1
1st and 15th of every month 1,15
every monday 1
1st monday of every month 1#1
ever other friday 2 5
every 6 months on the 5th 6 5
last friday of every month 5L
last day every month L

notes

  • This gem is a work-in-progress.
  • Does not yet support all recurrence options. More coming.
  • Supports inheritance but will not pick up dynamic changes in parent class parameter mappings.

todo

  • Allow mapping of a single cron expression (string) field, rather than individual fields.

ice_cube_model's People

Contributors

mattnichols avatar

Stargazers

Marco Roth avatar

Watchers

 avatar James Cloos avatar  avatar

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.