forked from woahdae/tire
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ACTIVEMODEL] Added the support for casting model properties as Ruby …
…objects In Tire::Persistence, you define a model property to be casted as a custom Ruby class instance. Currently, the implementation expects your class to take a Hash of attributes on initialization. (There are plans to support custom initialization logic.) You can cast either single values (see `Author` in the example below), or collections of values (see `[Comment]`). The behaviour was inspired by the CouchRest-Model gem. Also, all strings which conform to the UTC time format are automatically converted to Time objects. Also, all Hashes are automatically converted to Hashr [https://rubygems.org/gems/hashr] instances, allowing easy "dot-style" access to nested hash properties. Example: class Article include Tire::Model::Persistence validates_presence_of :title, :author property :title, :analyzer => 'snowball' property :published_on, :type => 'date' property :tags, :default => [], :analyzer => 'keyword' property :author, :class => Author property :comments, :class => [Comment] end See the test suite for more information.
- Loading branch information
Showing
5 changed files
with
104 additions
and
12 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
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 @@ | ||
class Author | ||
attr_accessor :first_name, :last_name | ||
def initialize(attributes) | ||
@first_name = HashWithIndifferentAccess.new(attributes)[:first_name] | ||
@last_name = HashWithIndifferentAccess.new(attributes)[:last_name] | ||
end | ||
end | ||
|
||
class Comment | ||
def initialize(params); @attributes = HashWithIndifferentAccess.new(params); end | ||
def method_missing(method_name, *arguments); @attributes[method_name]; end | ||
def as_json(*); @attributes; end | ||
end | ||
|
||
class PersistentArticleWithCastedItem | ||
include Tire::Model::Persistence | ||
|
||
property :title | ||
property :author, :class => Author | ||
property :stats | ||
end | ||
|
||
class PersistentArticleWithCastedCollection | ||
include Tire::Model::Persistence | ||
|
||
property :title | ||
property :comments, :class => [Comment] | ||
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
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