Class: Ohm::Utils::Upgrade

Inherits:
Object show all
Defined in:
lib/ohm/utils/upgrade.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Upgrade) initialize(models)

A new instance of Upgrade



17
18
19
20
# File 'lib/ohm/utils/upgrade.rb', line 17

def initialize(models)
  @models = models
  @types = Hash.new { |hash, model| hash[model] = {} }
end

Instance Attribute Details

- (Object) models (readonly)

Returns the value of attribute models



14
15
16
# File 'lib/ohm/utils/upgrade.rb', line 14

def models
  @models
end

- (Object) types (readonly)

Returns the value of attribute types



15
16
17
# File 'lib/ohm/utils/upgrade.rb', line 15

def types
  @types
end

Instance Method Details

- (Object) redis



10
11
12
# File 'lib/ohm/utils/upgrade.rb', line 10

def redis
  Ohm.redis
end

- (Object) run



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ohm/utils/upgrade.rb', line 22

def run
  models.each do |model|
    ns = Ohm::Key.new(model, redis)

    puts "Upgrading #{model}..."

    Batch.each(ns[:all].smembers) do |id|
      instance = ns[id]

      attrs = []
      deletes = []

      redis.keys(instance["*"]).each do |key|
        field = key[instance.size.succ..-1]

        type = (types[model][field] ||= redis.type(key).to_sym)

        if type == :string
          attrs << field
          attrs << redis.get(key)
          deletes << key
        end
      end

      redis.hmset(instance, *attrs)
      redis.del(*deletes)
    end
  end
end