Class: Ohm::Model::Collection
Instance Attribute Summary (collapse)
-
- (Object) model
readonly
Returns the value of attribute model.
-
- (Object) raw
readonly
Returns the value of attribute raw.
Instance Method Summary (collapse)
- - (Object) <<(model) (also: #add)
- - (Object) [](index)
- - (Object) all (also: #to_a)
- - (Object) clear
- - (Object) concat(models)
- - (Object) delete(model)
- - (Object) each(&block)
- - (Boolean) empty?
- - (Object) first(options = {})
- - (Boolean) include?(model)
-
- (Collection) initialize(key, model, db = nil)
constructor
A new instance of Collection.
- - (Object) key
- - (Object) replace(models)
- - (Object) size
- - (Object) sort(*args)
-
- (Object) sort_by(att, options = {})
Sort the model instances by the given attribute.
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?
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( = {}) if [:by] sort_by(.delete(:by), .merge(:limit => 1)).first else model[raw.first()] end end |
- (Boolean) include?(model)
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.
150 151 152 153 154 155 156 157 158 |
# File 'lib/ohm.rb', line 150 def sort_by(att, = {}) .merge!(:by => model.key("*->#{att}")) if [:get] raw.sort(.merge(:get => model.key("*->#{[:get]}"))) else sort() end end |