Module: Riagent::Persistence::ClassMethods

Defined in:
lib/riagent/persistence.rb

Instance Method Summary collapse

Instance Method Details

#all_for_field(field_name, results_limit = 1000) ⇒ Array

Returns all documents (within results/pagination limit)

from a collection.

Implemented instead of all() to get around current RiakJson API limitations

Parameters:

  • Name (String, Symbol)

    of a document field

  • Optional (Integer)

    results limit

Returns:

  • (Array)

    of ActiveDocument instances



93
94
95
96
97
98
99
100
101
102
# File 'lib/riagent/persistence.rb', line 93

def all_for_field(field_name, results_limit=1000)
  query = {
      field_name => {'$regex' => "/.*/"},
      '$per_page'=>results_limit
  }.to_json
  result = self.collection.find_all(query)
  if result.present?
    result.documents.map {|doc| self.from_rj_document(doc, persisted=true) }
  end
end

#collection_type(coll_type) ⇒ Object

Determines the document’s persistence strategy Valid options: [:riak_json] Usage: <code> class SomeModel

include Riagent::ActiveDocument
collection_type :riak_json  # persist to a RiakJson::Collection

end </code>



113
114
115
116
117
118
119
120
# File 'lib/riagent/persistence.rb', line 113

def collection_type(coll_type)
  @collection_type = coll_type
  case @collection_type
  when :riak_json
    self.persistence_strategy = :riak_json
    include Riagent::Persistence::RiakJsonStrategy
  end
end

#find(key) ⇒ Object

Load a document by key.



123
124
125
126
127
# File 'lib/riagent/persistence.rb', line 123

def find(key)
  return nil if key.nil? or key.empty?
  doc = self.collection.find_by_key(key)
  self.from_rj_document(doc, persisted=true)
end

#find_one(query) ⇒ Object

Return the first document that matches the query



130
131
132
133
134
135
136
137
138
# File 'lib/riagent/persistence.rb', line 130

def find_one(query)
  if query.kind_of? Hash
    query = query.to_json
  end
  doc = self.collection.find_one(query)
  if doc.present?
    self.from_rj_document(doc, persisted=true) 
  end
end

#get_collection_typeObject



140
141
142
# File 'lib/riagent/persistence.rb', line 140

def get_collection_type
  @collection_type ||= nil
end

#persistence_strategyObject



144
145
146
# File 'lib/riagent/persistence.rb', line 144

def persistence_strategy
  @persistence_strategy ||= nil
end

#persistence_strategy=(strategy) ⇒ Object



148
149
150
# File 'lib/riagent/persistence.rb', line 148

def persistence_strategy=(strategy)
  @persistence_strategy = strategy
end

#where(query) ⇒ Object

Return all documents that match the query



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/riagent/persistence.rb', line 153

def where(query)
  if query.kind_of? Hash
    query = query.to_json
  end
  result = self.collection.find_all(query)
  if result.present?
    result.documents.map do |doc| 
      self.from_rj_document(doc, persisted=true)
    end
  end
end