Sha256: 2891f02e6a1751a68e64db813db6e41a1dee5741bf50eb8081c63845c3632d90

Contents?: true

Size: 931 Bytes

Versions: 2

Compression:

Stored size: 931 Bytes

Contents

require 'ohm'
require 'securerandom'

module Minuteman
  class User < ::Ohm::Model
    attribute :uid
    attribute :identifier
    attribute :anonymous

    unique :uid
    unique :identifier

    index :anonymous

    def save
      self.uid ||= SecureRandom.uuid
      self.anonymous ||= !identifier
      super
    end

    def track(action, time = Time.now.utc)
      Minuteman.track(action, self, time)
    end

    def add(action, time = Time.now.utc)
      Minuteman.add(action, time, self)
    end

    def count(action, time = Time.now.utc)
      Minuteman::Analyzer.new(action, Minuteman::Counter::User, self)
    end

    def anonymous?
      self.anonymous == true
    end

    def promote(identifier)
      self.identifier = identifier
      self.anonymous = false
      save
    end

    def self.[](identifier_or_uuid)
      with(:uid, identifier_or_uuid) || with(:identifier, identifier_or_uuid)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minuteman-3.0.0 lib/minuteman/user.rb
minuteman-2.0.0 lib/minuteman/user.rb