Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch for mongoid #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/zodiac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def self.each_sign(&block)

require 'zodiac/date'
require 'zodiac/activerecord'
require 'zodiac/mongoid'
69 changes: 69 additions & 0 deletions lib/zodiac/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module Zodiac
module Mongoid
module InstanceMethods
def zodiac_sign
raise 'You should call #zodiac_reader in your class for this to work' unless self.class.respond_to?(:date_for_zodiac)
self.send(self.class.date_for_zodiac).try(:zodiac_sign)
end

Zodiac.each_sign do |symbol, integer|
method_name = "#{symbol}?"
define_method(method_name) do
raise 'You should call #zodiac_reader in your class for this to work' unless self.class.respond_to?(:date_for_zodiac)
self.send(self.class.date_for_zodiac).try(method_name)
end
end
private
def update_sign_id
sign_id_method = "#{self.class.zodiac_sign_id_field}="
new_sign_id = send(self.class.date_for_zodiac).try(:zodiac_sign_id)
send(sign_id_method, new_sign_id)
end
end

module ClassMethods
attr_accessor :date_for_zodiac, :zodiac_sign_id_field

def zodiac_reader(dob_attribute, options = {:sign_id_attribute => :zodiac_sign_id})
@date_for_zodiac = dob_attribute
@zodiac_sign_id_field = options[:sign_id_attribute]

# if the zodiac_sign_id attribute is present, we should update the sign attribute before each save
# and define some scopes
columns = self.fields.collect { |field| field[0].to_s }
if columns.include?(@zodiac_sign_id_field.to_s)
"registering Zodiac before_save"
self.before_save do |object|
object.send(:update_sign_id)
end

# Person.by_zodiac(7 || :libra) == Person.where(:zodiac_sign_id => 7)
scope :by_zodiac, lambda { |sign|
case sign
when Symbol
where(self.zodiac_sign_id_field => Zodiac::Finder::SIGN_IDS[sign])
when Fixnum
where(self.zodiac_sign_id_field => sign)
else
raise ArgumentError, "Invalid attribute type #{sign.class} for #{self}.by_zodiac"
end
}

# Person.gemini == Person.by_zodiac(3)
Zodiac.each_sign do |symbol, integer|
scope symbol, by_zodiac(integer)
end
end
end
end

def self.included(base)
base.send :include, InstanceMethods
base.extend ClassMethods
end
end
end

if defined?(::Mongoid)
Mongoid::Document.send(:include, Zodiac::Mongoid)
end