Skip to content

Commit

Permalink
Added the Tire::Model::DynamicPersistence extension
Browse files Browse the repository at this point in the history
Related: karmi/retire#605

Closes #14
  • Loading branch information
davekinkead authored and karmi committed Mar 4, 2013
1 parent eace50c commit 8fe8f5e
Show file tree
Hide file tree
Showing 16 changed files with 826 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Expand Up @@ -15,6 +15,10 @@ Then require the component you want to use in your application initializer. For
See specific files and folders inside the `lib/tire` folder for instructions and documentation.


### Dynamic Persistence ###

Adds support for dynamic attributes when using Tire::Model::Persistence so that model properties no longer need to be declared explicitly. [More info](lib/tire/model/dynamic_persistence).

### More Like This Queries ###

Adds support for [“more like this”](http://www.elasticsearch.org/guide/reference/query-dsl/mlt-query.html) queries.
Expand Down
34 changes: 34 additions & 0 deletions lib/tire/model/dynamic_persistence.rb
@@ -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'
28 changes: 28 additions & 0 deletions lib/tire/model/dynamic_persistence/README.markdown
@@ -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'

21 changes: 21 additions & 0 deletions lib/tire/model/dynamic_persistence/dynamic_persistence.rb
@@ -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
26 changes: 26 additions & 0 deletions test/dynamic_persistence/dynamic_persistence_test.rb
@@ -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

0 comments on commit 8fe8f5e

Please sign in to comment.