Module: Ohm::Sorted::ClassMethods

Defined in:
lib/ohm/sorted.rb

Instance Method Summary collapse

Instance Method Details

#sorted(attribute, options = {}) ⇒ Object



151
152
153
# File 'lib/ohm/sorted.rb', line 151

def sorted(attribute, options={})
  sorted_indices << [attribute, options]
end

#sorted_find(attribute, dict = {}) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/ohm/sorted.rb', line 159

def sorted_find(attribute, dict={})
  unless sorted_index_exists?(attribute, to_options(dict))
    raise index_not_found(attribute)
  end

  index_key = sorted_index_key(attribute, dict)
  Ohm::SortedSet.new(index_key, key, self)
end

#sorted_index_exists?(attribute, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/ohm/sorted.rb', line 168

def sorted_index_exists?(attribute, options=nil)
  !!sorted_indices.detect { |i| i == [attribute, options] }
end

#sorted_index_key(attribute, dict = {}) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/ohm/sorted.rb', line 172

def sorted_index_key(attribute, dict={})
  index_key = [key, "sorted", attribute]
  if dict.size == 1
    index_key.concat(dict.first)
  elsif dict.keys.size > 1
    raise ArgumentError
  end
  index_key.join(":")
end

#sorted_indicesObject



155
156
157
# File 'lib/ohm/sorted.rb', line 155

def sorted_indices
  @sorted_indices ||= []
end