Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the
Tire::Model::DynamicPersistence
extension
Related: karmi/retire#605 Closes #14
- Loading branch information
1 parent
eace50c
commit 8fe8f5e
Showing
16 changed files
with
826 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Dynamic Persistence | ||
# =================== | ||
# | ||
# 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 | ||
# | ||
# | ||
# Usage: | ||
# ------ | ||
# | ||
# Require the component: | ||
# | ||
# require 'tire/model/dynamic_persistence' | ||
# | ||
# Example: | ||
# ------- | ||
# | ||
# class Author | ||
# | ||
# include Tire::Model::Persistence | ||
# include Tire::Model::DynamicPersistence | ||
# end | ||
# | ||
# author = Author.new :name => 'Inigo Montoya', | ||
# :books => ['The Pragmatic Swordfighter', 'Revenge: Best Served Cold'] | ||
# | ||
# author.name | ||
# # => 'Inigo Montoya' | ||
# | ||
# | ||
require 'tire/model/dynamic_persistence/dynamic_persistence' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Dynamic Persistence | ||
|
||
Adds support for truly dynamic persistence models so that you no longer have to explicitly declare properties. | ||
|
||
Very useful if you want to create models on the fly. | ||
|
||
## Usage | ||
|
||
Require the module in your model file | ||
|
||
require 'tire/model/dynamic_persistence' | ||
|
||
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', | ||
:books => ['The Pragmatic Swordfighter', 'Revenge: Best Served Cold'] | ||
|
||
author.name | ||
# => 'Inigo Montoya' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'tire' | ||
|
||
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' | ||
def initialize(attrs={}) | ||
attrs.each do |attr, value| | ||
# => call Tire's property method if it hasn't been set | ||
self.class.property attr unless self.class.property_types.keys.include? attr | ||
# => set instance variable | ||
instance_variable_set("@#{attr}", value) | ||
end | ||
super attrs | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'test_helper' | ||
|
||
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' | ||
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 | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.