Permalink
Browse files

Cleaned up the Tire::Model::DynamicPersistence extension

Related: #14
  • Loading branch information...
1 parent 8fe8f5e commit 3c88aa7df1d92101b8191f998648530d2a0be508 @karmi committed Mar 4, 2013
@@ -4,8 +4,8 @@
# Author: Dave Kinkead <dave@kinkead.com.au>
#
#
-# Adds support for dynamic persistence to Tire::Model::Persistence so that explict
-# declarations of 'property :attr_name' are not required
+# Adds support for dynamic persistence to Tire::Model::Persistence,
+# so explicit declarations of `property :attr_name` are not required.
#
#
# Usage:
@@ -19,16 +19,15 @@
# -------
#
# class Author
-#
# include Tire::Model::Persistence
# include Tire::Model::DynamicPersistence
# end
#
-# author = Author.new :name => 'Inigo Montoya',
+# author = Author.new :name => 'Inigo Montoya',
# :books => ['The Pragmatic Swordfighter', 'Revenge: Best Served Cold']
#
-# author.name
+# author.name
# # => 'Inigo Montoya'
#
#
-require 'tire/model/dynamic_persistence/dynamic_persistence'
+require 'tire/model/dynamic_persistence/dynamic_persistence'
@@ -13,16 +13,14 @@ Require the module in your model file
Include Persistence and DynamicPersistence
class Author
-
include Tire::Model::Persistence
include Tire::Model::DynamicPersistence
end
Then create your model by passing it a hash of key:value pairs
- author = Author.new :name => 'Inigo Montoya',
+ author = Author.new :name => 'Inigo Montoya',
:books => ['The Pragmatic Swordfighter', 'Revenge: Best Served Cold']
- author.name
+ author.name
# => 'Inigo Montoya'
-
@@ -3,19 +3,20 @@
module Tire
module Model
module DynamicPersistence
-
+
# Overrides the initializer in Tire::Model::Persistence to allow
- # dynamic creation of attributes without the need to
- # declare them with 'property :name'
+ # dynamic creation of attributes without the need to
+ # declare them with `property :name`
+ #
def initialize(attrs={})
attrs.each do |attr, value|
- # => call Tire's property method if it hasn't been set
+ # => call Tire's property method if it's declared for this attribute
self.class.property attr unless self.class.property_types.keys.include? attr
- # => set instance variable
- instance_variable_set("@#{attr}", value)
+ # => set instance variable for this attribute
+ instance_variable_set("@#{attr}", value)
end
- super attrs
+ super attrs
end
end
end
-end
+end
@@ -1,24 +1,47 @@
require 'test_helper'
+require 'tire/model/dynamic_persistence'
+
+class DynamicAuthor
+ include Tire::Model::Persistence
+ include Tire::Model::DynamicPersistence
+end
+
+class PersistentArticleWithDynamicCreation
+
+ include Tire::Model::Persistence
+ include Tire::Model::DynamicPersistence
+
+ property :author, :class => DynamicAuthor
+ property :tags, :default => []
+end
+
module Tire
module Model
class DynamicPersistenceTest < Test::Unit::TestCase
context "Persistent model with dynamic creation" do
-
- should "permit access to attrs passed to create" do
- @article = PersistentArticleWithDynamicCreation.new :name => 'Elasticsearch', :title => 'You know, for Search!'
- assert_equal @article.name, 'Elasticsearch'
+
+ should "allow accessing attributes not explicitely defined" do
+ @article = PersistentArticleWithDynamicCreation.new :name => 'Elasticsearch',
+ :title => 'You know, for Search!'
+ assert_nothing_raised do
+ assert_equal 'Elasticsearch', @article.name
+ end
end
-
- should "not override explicit persistent properties" do
- @article = PersistentArticleWithDynamicCreation.new :name => 'Elasticsearch', :author => { :name => 'Inigo Montoya' }
- assert_equal @article.author.name, 'Inigo Montoya'
- assert_equal @article.tags.class, Array
- assert_equal @article.tags.length, 0
+
+ should "not override explicitely defined properties" do
+ @article = PersistentArticleWithDynamicCreation.new :name => 'Elasticsearch',
+ :author => { :name => 'Inigo Montoya' }
+
+ assert_instance_of DynamicAuthor, @article.author
+ assert_equal 'Inigo Montoya', @article.author.name
+
+ assert_instance_of Array, @article.tags
+ assert_equal [], @article.tags
end
-
+
end
end
Oops, something went wrong.

0 comments on commit 3c88aa7

Please sign in to comment.