Module: Riagent::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/riagent/persistence.rb,
lib/riagent/persistence/riak_kv_strategy.rb,
lib/riagent/persistence/persistence_strategy.rb,
lib/riagent/persistence/riak_dt_set_strategy.rb,
lib/riagent/persistence/riak_no_index_strategy.rb

Overview

Provides a common persistence API for Riagent Documents. Most persistence calls are delegated to the Collection class instance, which are implemented in persistence/*_strategy.rb modules.

Defined Under Namespace

Modules: ClassMethods Classes: PersistenceStrategy, RiakDTSetStrategy, RiakKVStrategy, RiakNoIndexStrategy

Constant Summary collapse

COLLECTION_TYPES =
[:riak_kv]
VALID_KEY_LISTS =

Key Listing strategies for :riak_kv collections

[:streaming_list_keys, :riak_dt_set]

Instance Method Summary collapse

Instance Method Details

#destroyObject

Delete the document from its collection



44
45
46
47
48
49
50
# File 'lib/riagent/persistence.rb', line 44

def destroy
  return nil if self.new_record?
  run_callbacks(:destroy) do
    self.class.persistence.remove(self)
    @destroyed = true
  end
end

#save(options = {:validate => true}) ⇒ String

Performs validations and saves the document The validation process can be skipped by passing validate: false. Also triggers :before_create / :after_create type callbacks

Returns:

  • (String)

    Returns the key for the inserted document



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/riagent/persistence.rb', line 56

def save(options={:validate => true})
  context = self.new_record? ? :create : :update
  return false if options[:validate] && !valid?(context)
  
  run_callbacks(context) do
    if context == :create
      key = self.class.persistence.insert(self)
    else
      key = self.class.persistence.update(self)
    end
    self.persist!
    key
  end
end

#save!(options = {:validate => true}) ⇒ Object

Attempts to validate and save the document just like save but will raise a Riagent::InvalidDocumentError exception instead of returning false if the doc is not valid.



73
74
75
76
77
78
# File 'lib/riagent/persistence.rb', line 73

def save!(options={:validate => true})
  unless save(options)
    raise Riagent::InvalidDocumentError.new(self)
  end
  true
end

#update(attrs) ⇒ Object

Update an object’s attributes and save it



81
82
83
84
85
86
# File 'lib/riagent/persistence.rb', line 81

def update(attrs)
  run_callbacks(:update) do
    self.attributes = attrs
    self.save
  end
end

#update!(attrs) ⇒ Object

Perform an update(), raise an error if the doc is not valid



89
90
91
92
93
94
# File 'lib/riagent/persistence.rb', line 89

def update!(attrs)
  unless update(attrs)
    raise Riagent::InvalidDocumentError.new(self)
  end
  true
end

#update_attributes(attrs) ⇒ Object

Update attributes (alias for update() for Rails versions < 4)



97
98
99
# File 'lib/riagent/persistence.rb', line 97

def update_attributes(attrs)
  self.update(attrs)
end