Module: Tire::Model::Persistence

Defined in:
lib/tire/model/persistence.rb,
lib/tire/model/persistence/finders.rb,
lib/tire/model/persistence/storage.rb,
lib/tire/model/persistence/attributes.rb

Overview

Allows to use Elasticsearch as a primary database (storage).

Contains all the Tire::Model::Search features and provides an ActiveModel-compatible interface for persistance.

Usage:

class Article
  include Tire::Model::Persistence

  property :title
end

Article.create :id => 1, :title => 'One'

article = Article.find

article.destroy

Defined Under Namespace

Modules: Attributes, Finders, Storage

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tire/model/persistence.rb', line 26

def self.included(base)

  base.class_eval do
    include ActiveModel::AttributeMethods
    include ActiveModel::Validations
    include ActiveModel::Serialization
    include ActiveModel::Serializers::JSON
    include ActiveModel::Naming
    include ActiveModel::Conversion

    extend  ActiveModel::Callbacks
    define_model_callbacks :save, :destroy

    extend  Persistence::Finders::ClassMethods
    extend  Persistence::Attributes::ClassMethods
    include Persistence::Attributes::InstanceMethods
    include Persistence::Storage

    include Tire::Model::Search

    ['_score', '_type', '_index', '_version', 'sort', 'highlight', '_explanation'].each do |attr|
      define_method("#{attr}=") { |value| @attributes ||= {}; @attributes[attr] = value }
      define_method("#{attr}")  { @attributes[attr] }
    end

    def self.search(*args, &block)
      args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
      args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }

      self.tire.search(*args, &block)
    end

    def self.multi_search(*args, &block)
      args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
      args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }

      self.tire.multi_search(*args, &block)
    end

  end

end