Class: Ohm::Model::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ohm.rb

Direct Known Subclasses

List, Set

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(key, model, db = nil)

A new instance of Collection



103
104
105
106
# File 'lib/ohm.rb', line 103

def initialize(key, model, db = nil)
  @model = model.unwrap
  @raw = self.class::Raw.new(key, db || @model.db)
end

Instance Attribute Details

- (Object) model (readonly)

Returns the value of attribute model



101
102
103
# File 'lib/ohm.rb', line 101

def model
  @model
end

- (Object) raw (readonly)

Returns the value of attribute raw



100
101
102
# File 'lib/ohm.rb', line 100

def raw
  @raw
end

Instance Method Details

- (Object) <<(model) Also known as: add



108
109
110
# File 'lib/ohm.rb', line 108

def <<(model)
  raw << model.id
end

- (Object) [](index)



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

def [](index)
  model[raw[index]]
end

- (Object) all Also known as: to_a



191
192
193
# File 'lib/ohm.rb', line 191

def all
  raw.to_a.map(&model)
end

- (Object) clear



165
166
167
# File 'lib/ohm.rb', line 165

def clear
  raw.clear
end

- (Object) concat(models)



169
170
171
172
# File 'lib/ohm.rb', line 169

def concat(models)
  raw.concat(models.map { |model| model.id })
  self
end

- (Object) delete(model)



160
161
162
163
# File 'lib/ohm.rb', line 160

def delete(model)
  raw.delete(model.id)
  model
end

- (Object) each(&block)



114
115
116
117
118
# File 'lib/ohm.rb', line 114

def each(&block)
  raw.each do |id|
    block.call(model[id])
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


183
184
185
# File 'lib/ohm.rb', line 183

def empty?
  raw.empty?
end

- (Object) first(options = {})



124
125
126
127
128
129
130
# File 'lib/ohm.rb', line 124

def first(options = {})
  if options[:by]
    sort_by(options.delete(:by), options.merge(:limit => 1)).first
  else
    model[raw.first(options)]
  end
end

- (Boolean) include?(model)

Returns:

  • (Boolean)


179
180
181
# File 'lib/ohm.rb', line 179

def include?(model)
  raw.include?(model.id)
end

- (Object) key



120
121
122
# File 'lib/ohm.rb', line 120

def key
  raw.key
end

- (Object) replace(models)



174
175
176
177
# File 'lib/ohm.rb', line 174

def replace(models)
  raw.replace(models.map { |model| model.id })
  self
end

- (Object) size



187
188
189
# File 'lib/ohm.rb', line 187

def size
  raw.size
end

- (Object) sort(*args)



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

def sort(*args)
  raw.sort(*args).map(&model)
end

- (Object) sort_by(att, options = {})

Sort the model instances by the given attribute.

Examples:

Sorting elements by name:

User.create :name => "B"
User.create :name => "A"

user = User.all.sort_by(:name, :order => "ALPHA").first
user.name == "A"
# => true


150
151
152
153
154
155
156
157
158
# File 'lib/ohm.rb', line 150

def sort_by(att, options = {})
  options.merge!(:by => model.key("*->#{att}"))

  if options[:get]
    raw.sort(options.merge(:get => model.key("*->#{options[:get]}")))
  else
    sort(options)
  end
end