Class: ActiveStore::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_store/base.rb

Defined Under Namespace

Classes: NoIdError

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



4
5
6
7
# File 'lib/active_store/base.rb', line 4

def initialize(params = {})
  set_attributes(params)
  @created_at ||= Time.now
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



71
72
73
# File 'lib/active_store/base.rb', line 71

def attributes
  @attributes
end

Class Method Details

.cas_update(id, ttl = default_ttl, &block) ⇒ Object

Allows you to update an ActiveStore object within a CAS “transaction”

Example:

Model.cas_update(3) do |instance|
  instance.value += 5
end

Returns nil if the object could not be found. Returns false if the CAS operation failed due to the object being modified in the background.



97
98
99
100
# File 'lib/active_store/base.rb', line 97

def cas_update(id, ttl = default_ttl, &block)
  block = attributes_block_wrapper(&block)
  connection.cas(id, ttl, &block)
end

.cas_update_or_create(id, ttl = default_ttl, &block) ⇒ Object

Like cas_update, but also creates a new object if none could be found. The given block is guaranteed to be executed at most once.

Example:

Model.cas_update_or_create(3) do |instance|
  instance.value ||= 0
  instance.value += 5
end

Returns false if the CAS operation failed due to the object having been modified by another process, or if someone managed to create an object between the CAS and ADD operations



115
116
117
118
# File 'lib/active_store/base.rb', line 115

def cas_update_or_create(id, ttl = default_ttl, &block)
  res = cas_update(id, ttl, &block)
  res.nil? ? create_with_block(id, ttl, &block) : res
end

.connectionObject



142
143
144
# File 'lib/active_store/base.rb', line 142

def connection
  @connection ||= Connection.new(connection_string, namespace, default_ttl)
end

.connection_stringObject



151
152
153
# File 'lib/active_store/base.rb', line 151

def connection_string
  @connection_string ||= (superclass.connection_string unless self == Base)
end

.connection_string=(connection_string) ⇒ Object



146
147
148
149
# File 'lib/active_store/base.rb', line 146

def connection_string= (connection_string)
  @connection = nil
  @connection_string = connection_string
end

.create(params = {}) ⇒ Object



80
81
82
83
84
# File 'lib/active_store/base.rb', line 80

def create(params = {})
  obj = new(params)
  obj.save!
  obj
end

.create_with_block(id, ttl = default_ttl, &block) ⇒ Object



127
128
129
130
# File 'lib/active_store/base.rb', line 127

def create_with_block(id, ttl = default_ttl, &block)
  block = attributes_block_wrapper(&block)
  connection.add(id, block.call({"id" => id}), ttl)
end

.default_ttlObject



169
170
171
# File 'lib/active_store/base.rb', line 169

def default_ttl
  @default_ttl ||= 30 * 24 * 3600
end

.default_ttl=(default_ttl) ⇒ Object



164
165
166
167
# File 'lib/active_store/base.rb', line 164

def default_ttl= (default_ttl)
  @connection = nil
  @default_ttl = default_ttl
end

.define_attributes(*args) ⇒ Object



73
74
75
76
77
78
# File 'lib/active_store/base.rb', line 73

def define_attributes(*args)
  @attributes ||= []
  @attributes += args.map(&:to_s)
  @attributes |= ["id", "created_at"]
  attr_accessor *@attributes
end

.find(id) ⇒ Object



132
133
134
135
136
# File 'lib/active_store/base.rb', line 132

def find(id)
  return nil if (id.nil? || id.empty?)
  params = connection.get(id)
  params ? new(params) : nil
end

.find_or_initialize_by_id(id) ⇒ Object



138
139
140
# File 'lib/active_store/base.rb', line 138

def find_or_initialize_by_id(id)
  find(id) || new(:id => id)
end

.namespaceObject



160
161
162
# File 'lib/active_store/base.rb', line 160

def namespace
  @namespace ||= self.name
end

.namespace=(namespace) ⇒ Object



155
156
157
158
# File 'lib/active_store/base.rb', line 155

def namespace= (namespace)
  @connection = nil
  @namespace = namespace
end

.stm_update_or_create(id, ttl = default_ttl, &block) ⇒ Object



120
121
122
123
124
125
# File 'lib/active_store/base.rb', line 120

def stm_update_or_create (id, ttl = default_ttl, &block)
  begin
    success = cas_update_or_create(id, ttl = default_ttl, &block)
  end until success
  true
end

Instance Method Details

#==(another_object) ⇒ Object



9
10
11
12
13
# File 'lib/active_store/base.rb', line 9

def ==(another_object)
  (self.class.attributes - ["created_at"]).all? do |attribute|
    self.send(attribute) == another_object.send(attribute)
  end
end

#attributesObject



15
16
17
18
19
# File 'lib/active_store/base.rb', line 15

def attributes
  self.class.attributes.inject({}) do |accu, attribute|
    send(attribute).nil? ? accu : accu.merge({attribute => send(attribute)})
  end
end

#connectionObject



56
57
58
# File 'lib/active_store/base.rb', line 56

def connection
  self.class.connection
end

#reloadObject

Raises:



40
41
42
43
44
# File 'lib/active_store/base.rb', line 40

def reload
  raise NoIdError.new("Could not reload without id.") if (id.nil? || id.empty?)
  set_attributes(connection.get(id))
  self
end

#save(ttl = self.class.default_ttl) ⇒ Object



46
47
48
49
50
# File 'lib/active_store/base.rb', line 46

def save(ttl = self.class.default_ttl)
  return false if (id.nil? || id.empty?)
  connection.set(id, self.attributes, ttl)
  true
end

#save!(ttl = self.class.default_ttl) ⇒ Object



52
53
54
# File 'lib/active_store/base.rb', line 52

def save!(ttl = self.class.default_ttl)
  save(ttl) or raise NoIdError.new("Could not save without id.")
end

#set_attributes(params = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/active_store/base.rb', line 21

def set_attributes(params = {})
  params = Hash[ params.map {|key, value| [key.to_s, value]} ]
  self.class.attributes.each do |attribute|
    write_attribute(attribute, params[attribute])
  end
end

#update_attribute(attribute, value) ⇒ Object



28
29
30
31
# File 'lib/active_store/base.rb', line 28

def update_attribute(attribute, value)
  self.send "#{attribute}=", value
  save
end

#update_attributes(params) ⇒ Object



33
34
35
36
37
38
# File 'lib/active_store/base.rb', line 33

def update_attributes(params)
  params.each do |key, value|
    send("#{key}=", value)
  end
  save
end