Class: Tarantool::Record
- Inherits:
-
BaseRecord
- Object
- BaseRecord
- 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
Class Method Summary collapse
- .define_field_accessor(name, type) ⇒ Object
- .set_space_no(val) ⇒ Object
- .set_tarantool(val) ⇒ Object
Instance Method Summary collapse
- #__fetched(attributes) ⇒ Object
- #_in_callbacks(&blk) ⇒ Object
- #destroy ⇒ Object
- #init(attributes) ⇒ Object
-
#initialize(attributes = {}) ⇒ Record
constructor
A new instance of Record.
- #save(and_reload = true) ⇒ Object
-
#update(ops) ⇒ Object
update record in db first, reload updated fileds then (Contrasting with LightRecord, where it reloads all fields) Consider that update operation does not count changes made by attr setters in your code, only field values in DB.
Constructor Details
#initialize(attributes = {}) ⇒ Record
Returns a new instance of Record.
75 76 77 78 79 80 81 |
# File 'lib/tarantool/record.rb', line 75 def initialize(attributes = {}) @__new_record = true @attributes = self.class.default_values.dup run_callbacks(:initialize) do init attributes end end |
Class Method Details
.define_field_accessor(name, type) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tarantool/record.rb', line 34 def define_field_accessor(name, type) generated_attribute_methods.class_eval <<-"EOF", __FILE__, __LINE__ - 1 def #{name} @attributes[:"#{name}"] end EOF if Symbol === type convert_code = case type when :int, :int64, :varint, :int16, :int8, :sint, :sint64, :sint16, :sint8 "v = v.to_i if String === v" when :string, :bytes "" else if Serializers::MAP[type] "v = Serializers::MAP[#{type.inspect}].decode(v) if String === v" else raise ArgumentError, "unknown field type #{type.inspect}" end end generated_attribute_methods.class_eval <<-"EOF", __FILE__, __LINE__ - 1 def #{name}=(v) #{convert_code} #{name}_will_change! unless v == @attributes[:"#{name}"] || new_record? @attributes[:"#{name}"] = v end EOF else generated_attribute_methods.class_eval do define_method("#{name}=") do |v| v = type.decode(v) if String === v send(:"#{name}_will_change!") unless v == @attributes[name] @attributes[name] = v end end end define_attribute_method name end |
.set_space_no(val) ⇒ Object
21 22 23 |
# File 'lib/tarantool/record.rb', line 21 def set_space_no(val) self.space_no = val end |
.set_tarantool(val) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/tarantool/record.rb', line 25 def set_tarantool(val) case val.class.name when 'Tarantool::BlockDB', 'Tarantool::FiberDB' self.tarantool = val else raise "Tarantool should be blocking of fibered!!! (i.e. of class Tarantool::BlockDB or Tarantool::FiberDB) (got #{val.class})" end end |
Instance Method Details
#__fetched(attributes) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/tarantool/record.rb', line 87 def __fetched(attributes) @__new_record = false # well, initalize callback could call #attributes @attributes = self.class.default_values.dup run_callbacks(:initialize) do @attributes = attributes end self end |
#_in_callbacks(&blk) ⇒ Object
97 98 99 100 101 |
# File 'lib/tarantool/record.rb', line 97 def _in_callbacks(&blk) run_callbacks(:save) { run_callbacks(new_record? ? :create : :update, &blk) } end |
#destroy ⇒ Object
158 159 160 161 162 163 |
# File 'lib/tarantool/record.rb', line 158 def destroy run_callbacks :destroy do self.class.delete id true end end |
#init(attributes) ⇒ Object
83 84 85 |
# File 'lib/tarantool/record.rb', line 83 def init(attributes) set_attributes(attributes) end |
#save(and_reload = true) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tarantool/record.rb', line 103 def save(and_reload = true) _in_callbacks do if valid? changes = changes() if new_record? if and_reload @attributes = space.insert(@attributes, return_tuple: true) else space.insert(@attributes) end else return true if changes.size == 0 ops = [] changes.each do |k, (old, new)| ops << [k.to_sym, :set, new] end if and_reload unless new_attrs = space.update(id, ops, return_tuple: true) _raise_doesnt_exists end else if space.update(id, ops) == 0 _raise_doesnt_exists end end end @previously_changed = changes @changed_attributes.clear old_record! true else false end end end |
#update(ops) ⇒ Object
update record in db first, reload updated fileds then (Contrasting with LightRecord, where it reloads all fields) Consider that update operation does not count changes made by attr setters in your code, only field values in DB.
record.update({:state => 'sleep', :sleep_count => [:+, 1]})
record.update([[:state, 'sleep'], [:sleep_count, :+, 1]])
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/tarantool/record.rb', line 146 def update(ops) raise UpdateNewRecord, "Could not call update on new record" if @__new_record unless new_attrs = space.update(id, ops, return_tuple: true) _raise_doesnt_exists end for op in ops field = op.flatten.first @attributes[field] = new_attrs[field] end self end |