Module: Riagent::Persistence::ClassMethods

Defined in:
lib/riagent/persistence.rb

Instance Method Summary collapse

Instance Method Details

#all(results_limit = 1000) ⇒ Array|nil

Return all the documents in the collection

Parameters:

  • results_limit (Integer) (defaults to: 1000)

    Number of results returned

Returns:

  • (Array|nil)

    of ActiveDocument instances



107
108
109
# File 'lib/riagent/persistence.rb', line 107

def all(results_limit=1000)
  self.persistence.all(results_limit)
end

#collection_type(coll_type, options = {}) ⇒ Object

Set the document’s persistence strategy Usage: <code> class SomeModel

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

end </code>



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/riagent/persistence.rb', line 119

def collection_type(coll_type, options={})
  unless COLLECTION_TYPES.include? coll_type
    raise ArgumentError, "Invalid collection type: #{coll_type.to_s}"
  end
  @collection_type = coll_type
  case @collection_type
  when :riak_json
    self.persistence = Riagent::Persistence::RiakJsonStrategy.new(self)
  when :riak_kv
    self.persistence = Riagent::Persistence::RiakKVStrategy.new(self)
    if options.has_key? :list_keys_using
      if options[:list_keys_using] == :streaming_list_keys
        self.persistence = Riagent::Persistence::RiakNoIndexStrategy.new(self)
      elsif options[:list_keys_using] == :riak_dt_set
        self.persistence = Riagent::Persistence::RiakDTSetStrategy.new(self)
      end
    end
  end
end

#find(key) ⇒ Object

Load a document by key.



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

def find(key)
  return nil if key.nil? or key.empty?
  self.persistence.find(key)
end

#find_one(query) ⇒ Object

Return the first document that matches the query



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

def find_one(query)
  unless self.persistence.allows_query?
    raise NotImplementedError, "This collection type does not support querying"
  end
  self.persistence.find_one(query)
end

#get_collection_typeObject



153
154
155
# File 'lib/riagent/persistence.rb', line 153

def get_collection_type
  @collection_type ||= nil
end

#persistenceObject



157
158
159
# File 'lib/riagent/persistence.rb', line 157

def persistence
  @persistence ||= nil
end

#persistence=(persistence_strategy) ⇒ Object



161
162
163
# File 'lib/riagent/persistence.rb', line 161

def persistence=(persistence_strategy)
  @persistence = persistence_strategy
end

#where(query) ⇒ Object

Return all documents that match the query



166
167
168
169
170
171
# File 'lib/riagent/persistence.rb', line 166

def where(query)
  unless self.persistence.allows_query?
    raise NotImplementedError, "This collection type does not support querying"
  end
  self.persistence.where(query)
end