Module: Enum::RedisStore::Setter

Defined in:
lib/enum/store/redis_store.rb

Class Method Summary collapse

Class Method Details

.namespace_name(name) ⇒ Object



80
81
82
83
# File 'lib/enum/store/redis_store.rb', line 80

def namespace_name(name)
  return name if name[0..@name.length] == @name
  "#@name.#{name}"
end

.nested_value_key(base, addy) ⇒ Object



76
77
78
# File 'lib/enum/store/redis_store.rb', line 76

def nested_value_key(base, addy)
  "nested.value:#{base}.#{addy}"
end

.raise_cannot_store_value_type(value) ⇒ Object



85
86
87
# File 'lib/enum/store/redis_store.rb', line 85

def raise_cannot_store_value_type(value)
  raise "Enum cannot store values of class #{value.class}."
end

.set_array_value(connection, key, value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/enum/store/redis_store.rb', line 46

def set_array_value(connection, key, value)
  connection.multi do
    value.each_with_index do |v, index|
      (connection.rpush(key, v) && next) if v.is_a?(String)
      nested_value_key(key, index).tap do |nvkey|
        set_value(connection, nvkey, v)
        connection.rpush(key, "ENUM_KEY:#{nvkey}")
      end
    end
  end
end

.set_hash_value(connection, key, value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/enum/store/redis_store.rb', line 58

def set_hash_value(connection, key, value)
  connection.multi do
    settable_hash = value.inject(Hash.new) do |out, pair|
      if pair.last.is_a?(String) || pair.last.is_a?(Fixnum)
        out[pair.first] = pair.last
      else
        nested_value_key(key, pair.first).tap do |nvkey|
          set_value(connection, nvkey, pair.last)
          out[pair.first] = "ENUM_KEY:#{nvkey}"
        end
      end
      out
    end

    connection.hmset(key, *settable_hash.to_a.flatten)
  end
end

.set_string_value(connection, key, value) ⇒ Object



42
43
44
# File 'lib/enum/store/redis_store.rb', line 42

def set_string_value(connection, key, value)
  connection.set(key, value)
end

.set_value(connection, key, value) ⇒ Object



35
36
37
38
39
40
# File 'lib/enum/store/redis_store.rb', line 35

def set_value(connection, key, value)
  meth = :"set_#{value.class.to_s.downcase}_value"
  raise_cannot_store_value_type(value) unless self.respond_to?(meth)

  send(meth, connection, key, value)
end