Class: Ricordami::UniqueIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/ricordami/unique_index.rb

Constant Summary collapse

SEPARATOR =
"_-::-_"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, fields, options = {}) ⇒ UniqueIndex

Returns a new instance of UniqueIndex.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ricordami/unique_index.rb', line 9

def initialize(model, fields, options = {})
  @model = model
  @fields = normalize_array(fields)
  @name = ("u_" + @fields.join("_")).to_sym
  @need_get_by = options[:get_by] && @fields != [:id]
  if options.has_key?(:scope)
    @scope = normalize_array(options[:scope])
    @fields.push(*@scope)
  end
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



7
8
9
# File 'lib/ricordami/unique_index.rb', line 7

def fields
  @fields
end

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/ricordami/unique_index.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ricordami/unique_index.rb', line 7

def name
  @name
end

#need_get_byObject (readonly)

Returns the value of attribute need_get_by.



7
8
9
# File 'lib/ricordami/unique_index.rb', line 7

def need_get_by
  @need_get_by
end

#scopeObject (readonly)

Returns the value of attribute scope.



7
8
9
# File 'lib/ricordami/unique_index.rb', line 7

def scope
  @scope
end

Instance Method Details

#add(id, value) ⇒ Object



28
29
30
31
32
# File 'lib/ricordami/unique_index.rb', line 28

def add(id, value)
  value = normalize_value(value)
  @model.redis.sadd(uidx_key_name, value)
  @model.redis.hset(ref_key_name, value, id) if @need_get_by
end

#allObject



51
52
53
# File 'lib/ricordami/unique_index.rb', line 51

def all
  @model.redis.smembers(uidx_key_name)
end

#countObject



55
56
57
# File 'lib/ricordami/unique_index.rb', line 55

def count
  @model.redis.scard(uidx_key_name)
end

#id_for_values(*values) ⇒ Object



46
47
48
49
# File 'lib/ricordami/unique_index.rb', line 46

def id_for_values(*values)
  values = values.flatten.join(SEPARATOR)
  @model.redis.hget(ref_key_name, values)
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ricordami/unique_index.rb', line 59

def include?(value)
  @model.redis.sismember(uidx_key_name, value)
end

#normalize_value(value) ⇒ Object



63
64
65
# File 'lib/ricordami/unique_index.rb', line 63

def normalize_value(value)
  value.is_a?(Array) ? value.join(SEPARATOR) : value
end

#ref_key_nameObject



24
25
26
# File 'lib/ricordami/unique_index.rb', line 24

def ref_key_name
  @ref_key_name ||= KeyNamer.hash_ref(@model, :fields => @fields)
end

#rem(id, value, return_command = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ricordami/unique_index.rb', line 34

def rem(id, value, return_command = false)
  if return_command
    commands = []
    commands << [:hdel, [ref_key_name, id]] if @need_get_by
    commands << [:srem, [uidx_key_name, value]]
    return commands
  end
  @model.redis.hdel(ref_key_name, id) if @need_get_by
  value = normalize_value(value)
  @model.redis.srem(uidx_key_name, value)
end

#uidx_key_nameObject



20
21
22
# File 'lib/ricordami/unique_index.rb', line 20

def uidx_key_name
  @uidx_key_name ||= KeyNamer.unique_index(@model, :name => @name)
end