Module: Ohm::Collection

Includes:
Enumerable
Included in:
List, Set
Defined in:
lib/ohm.rb,
lib/ohm/json.rb

Instance Method Summary collapse

Instance Method Details

#eachObject



120
121
122
123
124
125
126
127
128
# File 'lib/ohm.rb', line 120

def each
  if block_given?
    ids.each_slice(1000) do |slice|
      fetch(slice).each { |e| yield(e) }
    end
  else
    to_enum
  end
end

#empty?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/ohm.rb', line 135

def empty?
  size == 0
end

#fetch(ids) ⇒ Object

Wraps the whole pipelining functionality.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ohm.rb', line 140

def fetch(ids)
  data = nil

  model.synchronize do
    ids.each do |id|
      redis.queue("HGETALL", namespace[id])
    end

    data = redis.commit
  end

  return [] if data.nil?

  [].tap do |result|
    data.each_with_index do |atts, idx|
      result << model.new(Utils.dict(atts).update(:id => ids[idx]))
    end
  end
end

#to_aObject

Fetch the data from Redis in one go.



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

def to_a
  fetch(ids)
end

#to_json(*args) ⇒ Object

Sugar for to_a.to_json for all types of Sets



11
12
13
# File 'lib/ohm/json.rb', line 11

def to_json(*args)
  to_a.to_json(*args)
end