zodiac
is a simple gem for getting a zodiac sign from a given date of birth. It extends Time
, Date
and DateTime
objects with a #zodiac_sign
method and can also extend ActiveRecord::Base
with several instance (delegated to some date attribute of the model object) and class methods (allowing you to search for objects with a certain zodiac sign)
Installation
gem install zodiac
Or, if you want to extend your rails app, add the following to the Gemfile
:
gem 'zodiac'
and run bundle install
.
Usage
Time/Date/DateTime usage
require 'zodiac'
Time.now.zodiac_sign # => "Aries"
require 'date'
Date.new(2011, 1, 1).zodiac_sign # => "Capricorn"
DateTime.new(2011, 4, 30).zodiac_sign # => "Taurus"
#zodiac_sign
returns values using I18n
with "zodiac.#{sign}" path, so if you want your own translations, you can put them in your locale with keys like zodiac.aries
, zodiac.taurus
etc. See examples here.
There are also predicate methods which return true
if the date is matching the specified zodiac sign (and false
otherwise).
Date.new(1989, 2, 26).pisces? # => true
Time.gm(1978, 7, 12).gemini? # => false
ActiveRecord usage
The first thing you need to do is to add gem 'zodiac'
to your Gemfile
as described before.
To add zodiac methods to your model you just call a zodiac_reader
macro in your model passing it the name of the attribute containing the date of birth:
class Person < ActiveRecord::Base
zodiac_reader :dob
end
and then you'll be able to get zodiac sign of your object just by calling #zodiac_sign
on it:
@person = Person.first
@person.zodiac_sign # => "Taurus"
You can also use the predicate methods like #libra?
@person.libra? # => false
@person.taurus? # => true
If you also need to search for all geminis in your people
table, you should add an integer field containing a numerical id of the person's zodiac sign to that table. zodiac
can help you with that - it already includes a generator zodiac:migration
which creates a migration adding that field to your table (and an index on that field). You should specify the name of your model class as the first argument while calling the generator:
rails generate zodiac:migration Person
(Note that you must call zodiac_reader
in your model in order for the migration to run correctly - after creating a new field the migration tries to update it for all existing records)
Now zodiac_reader
macro in your model gives you some scopes to filter objects by a zodiac sign:
Person.with_zodiac_sign('libra') # returns all libras
Person.gemini # all the geminis
To keep the consistency of a zodiac sign with the date of birth, zodiac_reader
also installs a before_save
filter to your model, which updates the sign field every time you change the date-of-birth attribute.
If you don't like the name of the field containing zodiac sign (by default it's zodiac_sign_id
), you can customize it, passing the wanted name as an option to zodiac_reader
in your model and then as the second parameter to the generator:
class Person < ActiveRecord::Base
zodiac_reader :dob, sign_id_attribute: :custom_sign_id
end
rails generate zodiac:migration Person custom_sign_id
Included locales
- en (English)
- ru (Russian)
- pt-BR (Brazilian Portuguese) - thanks jeffrydegrande
- ja (Japanese) - thanks hamakn
- it (Italian) - thanks lucapette
- es (Spanish) - thanks jazminschroeder
- de (German) - thanks contradictioned
- zh-CN (Chinese) - thanks lufeihaidao
- fr (French) - thanks Belibaste
- lv (Latvian) - thanks ExClouds
- lt (Lithuanian) - thanks zilton7
- zh-TW (Traditional Chinese) - thanks Dylan0203
- uk (Ukrainian) - thanks valdemarua
- vi (Vietnamese) - thanks kevincolemaninc
Changelog
- 0.1 Initial version with Time/Date/DateTime extension and [:ru, :en] locales.
- 0.1.1 Added Brazilian Portuguese locale (thanks jeffrydegrande).
- 0.2 Added ActiveRecord support (scopes, predicate methods and delegating
#zodiac_sign
to date-of-birth attribute). - 0.2.1 Added Japanese locale (thanks hamakn).
- 0.2.2 Added Italian locale (thanks lucapette).
- 0.2.3 Added Spanish locale (thanks jazminschroeder).
- 0.2.4 Correct leap-year dates handling (thanks BastienDuplessier).
- 0.2.5 Added German locale (thanks contradictioned).
- 0.2.6 Added rails 4.0 support (thanks travisp).
- 0.2.7 Added Chinese, French & Latvian locales, added
Zodiac::Date#zodiac_sign_symbol
. - 0.2.8 Added Traditional Chinese locale (thanks Dylan0203).
- 0.2.9 Added Ukrainian locale (thanks valdemarua).
- 0.2.10 Added Vietnamese locale (thanks KevinColemanInc).
Roadmap
-
Rdoc/YARD coverage of everything
-
Other ORMs support (ROM, Sequel, Mongoid)
Contributing
Fork the repository, push your changes to a topic branch and send me a pull request.