Module: Norton::Timestamp::ClassMethods
- Defined in:
- lib/norton/timestamp.rb
Instance Method Summary collapse
-
#timestamp(name, options = {}) ⇒ type
[timestamp Define a timestamp].
Instance Method Details
#timestamp(name, options = {}) ⇒ type
- timestamp Define a timestamp
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 |
# File 'lib/norton/timestamp.rb', line 16 def (name, ={}) register_norton_value(name, :timestamp, ) redis = norton_value_redis_pool(name) # Redis: GET define_method(name) do return instance_variable_get("@#{name}") if instance_variable_defined?("@#{name}") value = redis.with do |conn| raw_value = conn.get(norton_value_key(name)) break raw_value if raw_value.present? send("#{name}_default_value").tap do |default_value| conn.set(norton_value_key(name), default_value) end end if value.nil? && [:allow_nil] instance_variable_set("@#{name}", nil) else instance_variable_set("@#{name}", value.to_i) end end define_method("#{name}_default_value") do return nil if [:allow_nil] return (Time.current.to_f * 1000).to_i if [:digits] == 13 Time.current.to_i end # Redis: SET define_method("touch_#{name}") do value = [:digits] == 13 ? (Time.current.to_f * 1000).to_i : Time.current.to_i redis.with do |conn| conn.set(norton_value_key(name), value) end instance_variable_set("@#{name}", value) end # Redis: DEL define_method("remove_#{name}") do redis.with do |conn| conn.del(norton_value_key(name)) end remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}") end send(:after_destroy, "remove_#{name}".to_sym) if respond_to? :after_destroy # Add callback if [:touch_on].present? [:touch_on].each do |callback, condition| self.send callback, proc{ if instance_eval(&condition) then instance_eval("touch_#{name}") end } end end end |