lib/norton/timestamp.rb
#
# [timestamp Define a timestamp]
# @param name [type] [description]
# @param touches={} [type] [description]
#
# @return [type] [description]
[timestamp Define a timestamp]
[ timestamp Define a timestamp ]
def timestamp(name, options={})
        register_norton_value(name, :timestamp, options)
        redis = norton_value_redis_pool(name)
        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? && options[: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 options[:allow_nil]
          return (Time.current.to_f * 1000).to_i if options[:digits] == 13
          Time.current.to_i
        end
        define_method("touch_#{name}") do
          value = options[: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
        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
        if options[:touch_on].present?
          options[:touch_on].each do |callback, condition|
            self.send callback, proc{ if instance_eval(&condition) then instance_eval("touch_#{name}") end }
          end
        end
      end
def timestamp ( name , options = { } ) register_norton_value ( name , :timestamp , options ) redis = norton_value_redis_pool ( name ) 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? && options [ :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 options [ :allow_nil ] return ( Time . current . to_f * 1000 ) . to_i if options [ :digits ] == 13 Time . current . to_i end define_method ( "touch_#{name}" ) do value = options [ :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 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 if options [ :touch_on ] . present? options [ :touch_on ] . each do | callback , condition | self . send callback , proc { if instance_eval ( & condition ) then instance_eval ( "touch_#{name}" ) end } end end end
[timestamp Define a timestamp]
[ timestamp Define a timestamp ]
# Redis: GET # Redis: SET # Redis: DEL # Add callback
None
type
name
None
options
touches={}
None