Class: Blackbeard::Storable

Inherits:
Object
  • Object
show all
Includes:
ConfigurationMethods, StorableAttributes, StorableHasMany, StorableHasSet
Defined in:
lib/blackbeard/storable.rb

Direct Known Subclasses

Cohort, Feature, Group, Metric, Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StorableAttributes

included

Methods included from StorableHasSet

included

Methods included from StorableHasMany

included

Methods included from ConfigurationMethods

#config, #db, #guest_method, included, #tz

Constructor Details

#initialize(id) ⇒ Storable

Returns a new instance of Storable.



47
48
49
50
# File 'lib/blackbeard/storable.rb', line 47

def initialize(id)
  @id = id.to_s.downcase
  @new_record = true
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



44
45
46
# File 'lib/blackbeard/storable.rb', line 44

def id
  @id
end

#new_recordObject

Returns the value of attribute new_record.



45
46
47
# File 'lib/blackbeard/storable.rb', line 45

def new_record
  @new_record
end

Class Method Details

.allObject



97
98
99
# File 'lib/blackbeard/storable.rb', line 97

def self.all
  all_keys.map{ |key| new_from_key(key) }
end

.all_keysObject



75
76
77
# File 'lib/blackbeard/storable.rb', line 75

def self.all_keys
  db.hash_keys(master_key)
end

.countObject



79
80
81
# File 'lib/blackbeard/storable.rb', line 79

def self.count
  db.hash_length(master_key)
end

.create(id, attributes = {}) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/blackbeard/storable.rb', line 60

def self.create(id, attributes = {})
  key = key_for(id)
  raise StorableDuplicateKey if db.hash_field_exists(master_key, key)
  storable = new(id)
  storable.save
  storable.update_attributes(attributes) unless attributes.empty?
  storable
end

.find(id) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/blackbeard/storable.rb', line 52

def self.find(id)
  key = key_for(id)
  return nil unless db.hash_field_exists(master_key, key)
  storable = new(id)
  storable.new_record = false
  storable
end

.find_or_create(id) ⇒ Object



69
70
71
72
73
# File 'lib/blackbeard/storable.rb', line 69

def self.find_or_create(id)
  storable = new(id)
  storable.save
  storable
end

.key_for(id) ⇒ Object



123
124
125
# File 'lib/blackbeard/storable.rb', line 123

def self.key_for(id)
  "#{master_key}::#{ id.to_s.downcase }"
end

.master_keyObject



15
16
17
18
19
# File 'lib/blackbeard/storable.rb', line 15

def master_key
  return @master_key if defined? @master_key
  return self.superclass.master_key if self.superclass.respond_to?(:master_key)
  raise StorableMasterKeyUndefined, "define master key in the class that inherits from storable"
end

.new_from_key(key) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/blackbeard/storable.rb', line 87

def self.new_from_key(key)
  if key =~ /^#{master_key}::(.+)$/
    storable = new($1)
    storable.new_record = false
    storable
  else
    nil
  end
end

.new_from_keys(*keys) ⇒ Object



83
84
85
# File 'lib/blackbeard/storable.rb', line 83

def self.new_from_keys(*keys)
  keys.flatten.map{ |key| new_from_key(key) }
end

.on_reload(method) ⇒ Object



30
31
32
# File 'lib/blackbeard/storable.rb', line 30

def on_reload(method)
  on_reload_methods.push(method)
end

.on_reload_methodsObject



34
35
36
37
# File 'lib/blackbeard/storable.rb', line 34

def on_reload_methods
  @on_reload_methods ||= self.superclass.on_reload_methods.dup if self.superclass.respond_to?(:on_reload_methods)
  @on_reload_methods ||= []
end

.on_save(method) ⇒ Object



21
22
23
# File 'lib/blackbeard/storable.rb', line 21

def on_save(method)
  on_save_methods.push(method)
end

.on_save_methodsObject



25
26
27
28
# File 'lib/blackbeard/storable.rb', line 25

def on_save_methods
  @on_save_methods ||= self.superclass.on_save_methods.dup if self.superclass.respond_to?(:on_save_methods)
  @on_save_methods ||= []
end

.set_master_key(master_key) ⇒ Object



11
12
13
# File 'lib/blackbeard/storable.rb', line 11

def set_master_key(master_key)
  @master_key = master_key.to_s
end

Instance Method Details

#==(o) ⇒ Object



119
120
121
# File 'lib/blackbeard/storable.rb', line 119

def ==(o)
  o.class == self.class && o.id == self.id
end

#keyObject



127
128
129
# File 'lib/blackbeard/storable.rb', line 127

def key
  self.class.key_for(id)
end

#master_keyObject



131
132
133
# File 'lib/blackbeard/storable.rb', line 131

def master_key
  self.class.master_key
end

#new_record?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/blackbeard/storable.rb', line 115

def new_record?
  @new_record
end

#reloadObject



110
111
112
113
# File 'lib/blackbeard/storable.rb', line 110

def reload
  self.class.on_reload_methods.each{ |m| self.send(m) }
  self
end

#saveObject



101
102
103
104
105
106
107
108
# File 'lib/blackbeard/storable.rb', line 101

def save
  if new_record?
    db.hash_key_set_if_not_exists(master_key, key, tz.now.to_date)
    @new_record = false
  end
  self.class.on_save_methods.each{ |m| self.send(m) }
  true
end