Coder Social home page Coder Social logo

calorie's Introduction

Calorie

A calendar is a calendar is a calendar.

This library abstracts away the fiddly logic for displaying a month-like calendar view. It's a presenter, I guess.

Motivation

The third time I found myself writing complicated view code to display a month-like calendar view I decided to go find a library that would abstract this away... and all the libraries I found contained a bunch of html and css stuff (and also depended on Rails), which was a bit more than I wanted.

So I re-invented another wheel (but made it really, really small).

Requirements

This uses Date from the Ruby Standard Library.

Usage

Create a hash that uses the day of the month as the key. The data can be anything; only your code will be interacting with it.

data = {
  1 => thing,
  2 => different_thing,
  3 => nil,
  4 => medium_sized_thing,
  5 => complicated_thing,
  6 => nil,
  # ... etc
}

Obviously, you don't need to pass in any data -- if it's easy enough to just loop through and check what day it is and do whatever you need to do in your template, that's up to you.

Create a calendar for a month by sending in year, month, and the data:

cal = Calorie.new(2010, 6, data)

In your template, you can style it however you wish.

# previous month, e.g. December 2009
cal.previous

# current month, e.g. January 2010
cal.current

# next month, e.g. February 2010
cal.next

cal.days_of_the_week do |day|
  # day.label
  # day.weekend?
end

cal.weeks.each do |week|
  # week.number
  week.days.each do |day|
    unless day.blank?
      # the day of the month is day.number
      # do stuff with day.data, which is
      # whatever you happened to put into the
      # data hash
      if day.today?
        # omg, it's RIGHT NOW
      end
    end
  end
end

Note: Weeks are numbered per ISO-8601. This gets a bit weird if you've configured your week to start on Sunday, as the ISO-8601 specifies that the week starts on Monday. On the other hand, the definition for week 1 is the week that has the year's first Thursday in it, so that can be defined unambiguously even if you start your week on a Sunday.

If you don't need to lay it out by week, you can also iterate straight through the days (though I'm not sure why you'd use Calorie for this):

cal.days.each do |day|
  # the day of the month is day.number
  # do stuff with day.data
  if day.today?
    # omg, it's RIGHT NOW
  end
end

Configuration

By default the first day of the week is Sunday, however this can be changed to Monday like so:

Calorie.configuration do |config|
  config.week_starts_on :monday
end

You will need to add translations for your locale(s). The translations for the week begin with Sunday, regardless of how you configure your week.

# en.yml
---
calorie:
  days_of_the_week: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
  months: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]



# fr.yml
---
calorie:
  days_of_the_week: ["di", "lu", "ma", "me", "je", "ve", "sa"]
  months: ["janv.", "fevr.", "mars", "avr.", "mai", "juin", "juil.", "aout", "sept.", "oct.", "nov.", "dec."]

calorie's People

Contributors

kytrinyx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.