Class: Tarantool::Record
- Inherits:
-
Object
- Object
- Tarantool::Record
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Dirty, ActiveModel::Serialization, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, ActiveModel::Validations
- Defined in:
- lib/tarantool/record.rb
Instance Attribute Summary collapse
-
#new_record ⇒ Object
Returns the value of attribute new_record.
Class Method Summary collapse
- ._cast(name, value) ⇒ Object
- ._get_serializer(type) ⇒ Object
- .create(attribites = {}) ⇒ Object
- .field(name, type, params = {}) ⇒ Object
- .find(*keys) ⇒ Object
- .from_server(tuple) ⇒ Object
- .hash_to_tuple(hash, with_nils = false) ⇒ Object
- .index(*fields) ⇒ Object
- .ordered_keys(keys) ⇒ Object
- .select ⇒ Object
- .set_space_no(val) ⇒ Object
- .set_tarantool(val) ⇒ Object
- .space ⇒ Object
- .tuple_to_hash(tuple) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #destroy ⇒ Object
- #field_no(name) ⇒ Object
- #id ⇒ Object
- #in_callbacks(&blk) ⇒ Object
- #increment(field, by = 1) ⇒ Object
-
#initialize(attributes = {}) ⇒ Record
constructor
A new instance of Record.
- #new_record! ⇒ Object
- #new_record? ⇒ Boolean
- #old_record! ⇒ Object
-
#reload ⇒ Object
return new object, not reloading itself as AR-model.
- #save ⇒ Object
- #space ⇒ Object
- #to_tuple ⇒ Object
- #update_attribute(field, value) ⇒ Object
- #update_attributes(attributes) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Record
Returns a new instance of Record.
228 229 230 231 232 233 |
# File 'lib/tarantool/record.rb', line 228 def initialize(attributes = {}) attributes.each do |k, v| send("#{k}=", v) end @new_record = true end |
Instance Attribute Details
#new_record ⇒ Object
Returns the value of attribute new_record.
227 228 229 |
# File 'lib/tarantool/record.rb', line 227 def new_record @new_record end |
Class Method Details
._cast(name, value) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/tarantool/record.rb', line 210 def _cast(name, value) type = self.fields[name][:type] serializer = _get_serializer(type) if value.is_a?(Field) return nil if value.data == "\0" serializer.decode(value) else return "\0" if value.nil? serializer.encode(value) end end |
._get_serializer(type) ⇒ Object
222 223 224 |
# File 'lib/tarantool/record.rb', line 222 def _get_serializer(type) Serializers::MAP[type] || raise(TarantoolError.new("Undefind serializer #{type}")) end |
.create(attribites = {}) ⇒ Object
173 174 175 |
# File 'lib/tarantool/record.rb', line 173 def create(attribites = {}) new(attribites).tap { |o| o.save } end |
.field(name, type, params = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tarantool/record.rb', line 127 def field(name, type, params = {}) define_attribute_method name self.fields = fields.merge name => { type: type, field_no: fields.size, params: params } unless self.primary_index self.primary_index = name index name end if params[:default] self.default_values = default_values.merge name => params[:default] end define_method name do attributes[name] end define_method "#{name}=" do |v| send("#{name}_will_change!") unless v == attributes[name] attributes[name] = v end end |
.find(*keys) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/tarantool/record.rb', line 150 def find(*keys) res = space.select(*keys) if keys.size == 1 if res.tuple from_server res.tuple else nil end else res.tuples.map { |tuple| from_server tuple } end end |
.from_server(tuple) ⇒ Object
177 178 179 |
# File 'lib/tarantool/record.rb', line 177 def from_server(tuple) new(tuple_to_hash(tuple)).tap { |v| v.old_record! } end |
.hash_to_tuple(hash, with_nils = false) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/tarantool/record.rb', line 192 def hash_to_tuple(hash, with_nils = false) res = [] fields.keys.each do |k| v = hash[k] res << _cast(k, v) if with_nils || !v.nil? end res end |
.index(*fields) ⇒ Object
146 147 148 |
# File 'lib/tarantool/record.rb', line 146 def index(*fields) self.indexes = (indexes.dup << fields).sort_by { |v| v.size } end |
.ordered_keys(keys) ⇒ Object
201 202 203 204 205 206 207 208 |
# File 'lib/tarantool/record.rb', line 201 def ordered_keys(keys) fields.keys.inject([]) do |memo, k| keys.each do |k2| memo << k2 if k2 == k end memo end end |
.select ⇒ Object
163 164 165 |
# File 'lib/tarantool/record.rb', line 163 def select Select.new(self) end |
.set_space_no(val) ⇒ Object
119 120 121 |
# File 'lib/tarantool/record.rb', line 119 def set_space_no(val) self.space_no = val end |
.set_tarantool(val) ⇒ Object
123 124 125 |
# File 'lib/tarantool/record.rb', line 123 def set_tarantool(val) self.tarantool = val end |
.space ⇒ Object
181 182 183 |
# File 'lib/tarantool/record.rb', line 181 def space @space ||= tarantool.space(space_no) end |
.tuple_to_hash(tuple) ⇒ Object
185 186 187 188 189 190 |
# File 'lib/tarantool/record.rb', line 185 def tuple_to_hash(tuple) fields.keys.zip(tuple).inject({}) do |memo, (k, v)| memo[k] = _cast(k, v) unless v.nil? memo end end |
Instance Method Details
#attributes ⇒ Object
247 248 249 |
# File 'lib/tarantool/record.rb', line 247 def attributes @attributes ||= self.class.default_values.dup end |
#destroy ⇒ Object
301 302 303 304 305 306 |
# File 'lib/tarantool/record.rb', line 301 def destroy run_callbacks :destroy do space.delete id true end end |
#field_no(name) ⇒ Object
312 313 314 |
# File 'lib/tarantool/record.rb', line 312 def field_no(name) self.class.fields[name][:field_no] end |
#id ⇒ Object
235 236 237 |
# File 'lib/tarantool/record.rb', line 235 def id attributes[self.class.primary_index] end |
#in_callbacks(&blk) ⇒ Object
260 261 262 |
# File 'lib/tarantool/record.rb', line 260 def in_callbacks(&blk) run_callbacks(:save) { run_callbacks(new_record? ? :create : :update, &blk)} end |
#increment(field, by = 1) ⇒ Object
297 298 299 |
# File 'lib/tarantool/record.rb', line 297 def increment(field, by = 1) space.update id, ops: [[field_no(field), :add, by]] end |
#new_record! ⇒ Object
251 252 253 |
# File 'lib/tarantool/record.rb', line 251 def new_record! @new_record = true end |
#new_record? ⇒ Boolean
243 244 245 |
# File 'lib/tarantool/record.rb', line 243 def new_record? @new_record end |
#old_record! ⇒ Object
255 256 257 |
# File 'lib/tarantool/record.rb', line 255 def old_record! @new_record = false end |
#reload ⇒ Object
return new object, not reloading itself as AR-model
317 318 319 |
# File 'lib/tarantool/record.rb', line 317 def reload self.class.find(id) end |
#save ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/tarantool/record.rb', line 259 def save def in_callbacks(&blk) run_callbacks(:save) { run_callbacks(new_record? ? :create : :update, &blk)} end in_callbacks do if valid? if new_record? space.insert(*to_tuple) else ops = changed.inject([]) do |memo, k| k = k.to_sym memo << [field_no(k), :set, self.class._cast(k, attributes[k])] if attributes[k] memo end space.update id, ops: ops end @previously_changed = changes @changed_attributes.clear old_record! true else false end end end |
#space ⇒ Object
239 240 241 |
# File 'lib/tarantool/record.rb', line 239 def space self.class.space end |
#to_tuple ⇒ Object
308 309 310 |
# File 'lib/tarantool/record.rb', line 308 def to_tuple self.class.hash_to_tuple attributes, true end |
#update_attribute(field, value) ⇒ Object
285 286 287 288 |
# File 'lib/tarantool/record.rb', line 285 def update_attribute(field, value) self.send("#{field}=", value) save end |
#update_attributes(attributes) ⇒ Object
290 291 292 293 294 295 |
# File 'lib/tarantool/record.rb', line 290 def update_attributes(attributes) attributes.each do |k, v| self.send("#{k}=", v) end save end |