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



105
106
107
# File 'lib/riagent/persistence.rb', line 105

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_kv,   # Persist to a Riak::Bucket 
                list_keys_using: :riak_dt_set  #keep track of keys in a Set CRDT data type

end </code>



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

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_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.



137
138
139
140
# File 'lib/riagent/persistence.rb', line 137

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



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

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



150
151
152
# File 'lib/riagent/persistence.rb', line 150

def get_collection_type
  @collection_type ||= nil
end

#persistenceObject



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

def persistence
  @persistence ||= nil
end

#persistence=(persistence_strategy) ⇒ Object



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

def persistence=(persistence_strategy)
  @persistence = persistence_strategy
end

#where(query) ⇒ Object

Return all documents that match the query



163
164
165
166
167
168
# File 'lib/riagent/persistence.rb', line 163

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