Browse files

Removing block arguments to simplify. Adding two-way denormalization.…

… Bumped gemspec.
  • Loading branch information...
1 parent 39f13df commit a6a65a78dca24d02c2606108aca2be2deae16919 @logandk logandk committed Jul 9, 2010
Showing with 82 additions and 108 deletions.
  1. +24 −32 README.md
  2. +26 −26 lib/mongoid_denormalize.rb
  3. +1 −1 mongoid_denormalize.gemspec
  4. +2 −2 spec/models/comment.rb
  5. +2 −2 spec/models/post.rb
  6. +2 −4 spec/models/user.rb
  7. +25 −41 spec/mongoid_denormalize_spec.rb
View
56 README.md
@@ -25,8 +25,7 @@ In your model:
include Mongoid::Denormalize
# Define your denormalized fields
- denormalize :title, :from => :post
- denormalize :name, :avatar, :from => :user
+ denormalize :name, :email, :from => :user
@@ -37,61 +36,54 @@ Example
include Mongoid::Document
include Mongoid::Denormalize
- references_many :posts
+ references_many :comments
field :name
- field :avatar
+ field :email
+
+ denormalize :name, :email, :to => :comments
end
- def Post
+ def Comment
include Mongoid::Document
include Mongoid::Denormalize
referenced_in :user
- field :title
- denormalize :name, :avatar, :from => :user
+ field :body
+
+ denormalize :name, :email, :from => :user
end
- >> user = User.create(:name => "John Doe", :avatar => "http://url/to/avatar.png")
- >> post = Post.create(:title => "Blog post", :user => user)
- >> post.user_name
+ >> user = User.create(:name => "John Doe", :email => "john@doe.com")
+ >> comment = Comment.create(:body => "Lorem ipsum...", :user => user)
+ >> user.comments << comment
+ >> comment.user_name
"John Doe"
- >> post.user_avatar
- "http://url/to/avatar.png"
+ >> comment.user_email
+ "john@doe.com"
>> user.update_attributes(:name => "Bill")
- >> post.save
- >> post.user_name
+ >> comment.user_name
"Bill"
Options
-------
-Denormalization can happen using an associated object as illustrated above, or using a block. The field type for denormalized fields must
-be explicitly set if it is not a `String` value. Examples:
+Denormalization can happen in either or both directions. When using the `:from` option, the associated objects will fetch the values from
+the parent. When using the `:to` option, the parent will push the values to its children.
- # Basic denormalization. Will set the user_name attribute with the user name.
+ # Basic denormalization. Will set the user_name attribute with the associated user's name.
denormalize :name, :from => :user
- # Override denormalized field name. Will set the from_email attribute with the user email.
- denormalize :email, :from => :user, :to => :from_email
-
- # Specify denormalized field type. Will set the post_created_at attribute as a Time object.
- denormalize :created_at, :type => Time, :from => :post
+ # Basic denormalization. Will set the user_name attribute of "self.comments" with "self.name".
+ denormalize :name, :to => :comments
- # Multiple denormalization fields. Will set the user_name and user_email attributes with values from user.
+ # Multiple fields. Will set the user_name and user_email attributes with the associated user's name and email.
denormalize :name, :email, :from => :user
- # Block denormalization. Will set the comment_count attribute with the blocks return value.
- # The block receives the current instance as the first argument.
- denormalize(:comment_count, :type => Integer) { |post| post.comments.count }
-
- # Block denormalization with multiple fields. Will set the post_titles and post_dates attributes with the blocks return value.
- # The block receives the current instance as the first argument and the name of the denormalized field as the second argument.
- denormalize :post_titles, :post_dates, :type => Array do |user, field|
- field == :post_titles ? user.posts.collect(&:title) : user.posts.collect(&:created_at)
- end
+ # Multiple children. Will set the user_name attribute of "self.posts" and "self.comments" with "self.name".
+ denormalize :name, :to => [:posts, :comments]
Credits
View
52 lib/mongoid_denormalize.rb
@@ -6,10 +6,13 @@ module Mongoid::Denormalize
included do
cattr_accessor :denormalize_definitions
+
+ before_save :denormalize_from
+ after_save :denormalize_to
end
module ClassMethods
- # Set a field or a number of fields to denormalize. Specify the associated object using the :from option.
+ # Set a field or a number of fields to denormalize. Specify the associated object using the :from or :to options.
#
# def Post
# include Mongoid::Document
@@ -19,43 +22,40 @@ module ClassMethods
# references_many :comments
#
# denormalize :name, :avatar, :from => :user
- #
- # denormalize :email, :from => :user, :to => :from_email
- #
- # denormalize :comment_count, :type => Integer do |post|
- # post.comments.count
- # end
+ # denormalize :created_at, :to => :comments
# end
- def denormalize(*fields, &block)
+ def denormalize(*fields)
options = fields.pop
- (self.denormalize_definitions ||= []) << { :fields => fields, :options => options, :block => block}
+ (self.denormalize_definitions ||= []) << { :fields => fields, :options => options }
# Define schema
- fields.each do |name|
- denormalized_name = if block_given?
- name
- else
- options[:to] ? options[:to] : "#{options[:from]}_#{name}"
- end
-
- field denormalized_name, :type => options[:type]
+ unless options[:to]
+ fields.each { |name| field "#{options[:from]}_#{name}", :type => options[:type] }
end
-
- before_validation :denormalize_fields
end
end
private
- def denormalize_fields
+ def denormalize_from
self.denormalize_definitions.each do |definition|
- definition[:fields].each do |name|
- if definition[:block]
- value = (definition[:fields].length > 1 ? definition[:block].call(self, name) : definition[:block].call(self))
- self.send("#{name}=", value)
+ next if definition[:options][:to]
+
+ definition[:fields].each { |name| self.send("#{definition[:options][:from]}_#{name}=", self.send(definition[:options][:from]).try(name)) }
+ end
+ end
+
+ def denormalize_to
+ self.denormalize_definitions.each do |definition|
+ next unless definition[:options][:to]
+
+ assigns = Hash[*definition[:fields].collect { |name| ["#{self.class.name.underscore}_#{name}", self.send(name)] }.flatten]
+
+ [definition[:options][:to]].flatten.each do |association|
+ if [:embedded_in, :embeds_one, :referenced_in, :references_one].include? self.class.reflect_on_association(association)
+ self.send(association).update_attributes(assigns) unless self.send(association).blank?
else
- attribute_name = (definition[:options][:to] ? definition[:options][:to] : "#{definition[:options][:from]}_#{name}")
- self.send("#{attribute_name}=", self.send(definition[:options][:from]).try(name))
+ self.send(association).to_a.each { |a| a.update_attributes(assigns) }
end
end
end
View
2 mongoid_denormalize.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{mongoid_denormalize}
- s.version = "0.0.1"
+ s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Logan Raarup"]
View
4 spec/models/comment.rb
@@ -4,10 +4,10 @@ class Comment
field :body
- referenced_in :post
+ embedded_in :post, :inverse_of => :comments
referenced_in :user
denormalize :name, :from => :user
- denormalize :email, :from => :user, :to => :from_email
+ denormalize :email, :from => :user
denormalize :created_at, :type => Time, :from => :post
end
View
4 spec/models/post.rb
@@ -7,8 +7,8 @@ class Post
field :created_at, :type => Time
referenced_in :user
- references_many :comments
+ embeds_many :comments
denormalize :name, :email, :from => :user
- denormalize(:comment_count, :type => Integer) { |post| post.comments.count }
+ denormalize :created_at, :to => :comments
end
View
6 spec/models/user.rb
@@ -5,10 +5,8 @@ class User
field :name
field :email
- references_many :posts
+ references_one :post
references_many :comments
- denormalize :post_titles, :post_dates, :type => Array do |user, field|
- field == :post_titles ? user.posts.collect(&:title) : user.posts.collect(&:created_at).collect { |t| t + 300 }
- end
+ denormalize :name, :email, :to => [:post, :comments]
end
View
66 spec/mongoid_denormalize_spec.rb
@@ -6,23 +6,21 @@
c.drop rescue nil
end
- @user = User.create!(:name => "John Doe", :email => "john@doe.com")
- @post = Post.create!(:title => "Blog post", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"), :user => @user)
- @comment = Comment.create!(:body => "This is the comment", :post => @post, :user => @user)
+ @post = Post.create!(:title => "Blog post", :body => "Lorem ipsum...", :created_at => Time.parse("Jan 1 2010 12:00"))
+ @user = User.create!(:name => "John Doe", :email => "john@doe.com", :post => @post)
+ @comment = @post.comments.create(:body => "This is the comment", :user => @user)
- @other_user = User.create!(:name => "Bill", :email => "bill@doe.com")
+ @user.comments << @comment
+
+ @other_user = User.create!(:name => "Bill")
end
- context "denormalize associated object" do
+ context "denormalize from" do
it "should define multiple fields for association" do
@post.fields.should have_key "user_name"
@post.fields.should have_key "user_email"
end
- it "should override the name of the denormalized field" do
- @comment.fields.should have_key "from_email"
- end
-
it "should default to string field type for associated fields" do
@post.fields["user_name"].type.should eql String
end
@@ -33,12 +31,12 @@
it "should allow multiple declarations for the same association" do
@comment.fields.should have_key "user_name"
- @comment.fields.should have_key "from_email"
+ @comment.fields.should have_key "user_email"
end
it "should denormalize fields without specified type" do
@comment.user_name.should eql @user.name
- @comment.from_email.should eql @user.email
+ @comment.user_email.should eql @user.email
@post.user_name.should eql @user.name
@post.user_email.should eql @user.email
end
@@ -47,50 +45,36 @@
@comment.post_created_at.should eql @post.created_at
end
- it "should update denormalized values if changed" do
+ it "should update denormalized values if attribute is changed" do
+ @user.update_attributes(:name => "Bob Doe")
+
+ @comment.user_name.should eql @user.name
+ end
+
+ it "should update denormalized values if object is changed" do
@other_user = User.create!(:name => "Bill", :email => "bill@doe.com")
@comment.user = @other_user
@comment.save!
@comment.user_name.should eql @other_user.name
- @comment.from_email.should eql @other_user.email
+ @comment.user_email.should eql @other_user.email
end
end
- context "denormalization with block" do
- it "should accept block for denormalization" do
- @post.fields.should have_key "comment_count"
- end
-
- it "should accept multiple fields for block" do
- @user.fields.should have_key "post_titles"
- @user.fields.should have_key "post_dates"
- end
-
- it "should allow setting the field type" do
- @user.fields["post_titles"].type.should eql Array
- @post.fields["comment_count"].type.should eql Integer
- end
-
- it "should denormalize fields using block" do
- @post.save!
- @post.comment_count.should eql 1
-
+ context "denormalize to" do
+ it "should push denormalized fields to one-to-one association" do
+ @user.name = "Elvis"
@user.save!
- @user.post_titles.should eql ["Blog post"]
- @user.post_dates.should eql [Time.parse("Jan 1 2010 12:00") + 300]
+
+ @post.user_name.should eql "Elvis"
end
- it "should update denormalized values if changed" do
- @post.user = @other_user
+ it "should push denormalized fields to one-to-many association" do
+ @post.created_at = Time.parse("Jan 1 2011 12:00")
@post.save!
- @user.save!
- @other_user.save!
-
- @user.post_titles.should eql []
- @other_user.post_titles.should eql ["Blog post"]
+ @comment.post_created_at.should eql Time.parse("Jan 1 2011 12:00")
end
end
end

0 comments on commit a6a65a7

Please sign in to comment.