module MongoMapper::Document::ClassMethods

Override Cequel::Record here

Public Instance Methods

fake_data_with(args, values, opts={}) click to toggle source

fakes up data

# File lib/octocore-mongo/models.rb, line 46
def fake_data_with(args, values, opts={})
  res = []
  ts = args.fetch(:ts, 7.days.ago..Time.now.floor)
  if ts.class == Range
    bod = opts.fetch(:bod, false)
    ts_begin = ts.begin
    ts_end = ts.end
    if bod
      ts_begin = ts_begin.beginning_of_day
      ts_end = ts_end.end_of_day
    end
    step = opts.fetch(:step, 1.minute)
    ts_begin.to(ts_end, step).each do |_ts|
      _args = args.merge({ ts: _ts })
      r = self.where(_args)
      if r.count == 0
        res << self.new(_args.merge(values)).save!
      else
        res << r
      end
    end
  elsif ts.class == Time
    _args = args.merge({ ts: ts }).merge(values)
    res << self.new(_args).save!
  end
  res.flatten
end
findOrCreate(args, options = {}) click to toggle source

Finds the record/recordset satisfying a `where` condition

or create a new record from the params passed

@param [Hash] args The args used to build `where` condition @param [Hash] options The options used to construct record

# File lib/octocore-mongo/models.rb, line 132
def findOrCreate(args, options = {})
  # attempt to find the record
  res = get_cached(args)

  # on failure, do
  unless res
    args.merge!(options)
    res = self.new(args).save!

    # Update cache
    cache_key = gen_cache_key(args)
    MongoMapper::Document.redis.setex(cache_key, get_ttl, Octo::Utils.serialize(res))
  end
  res
end
findOrCreateOrAdjust(args, options) click to toggle source

If a record exists in a COUNTER TABLE, it will find

it and increment or decrement it's value with the
provided options. Else, will just create the
record with default value.
# File lib/octocore-mongo/models.rb, line 153
def findOrCreateOrAdjust(args, options)
  self.where(args).data_set.increment(options)
end
findOrCreateOrUpdate(args, options = {}) click to toggle source

If a record exists, will find it and update it's value with the

provided options. Else, will just create the record.
# File lib/octocore-mongo/models.rb, line 92
def findOrCreateOrUpdate(args, options = {})
  cache_key = gen_cache_key(args)
  res = get_cached(args)
  if res
    dirty = false

    # handle price separately because of float issues
    if options.has_key?(:price)
      _v = options.delete(:price)
      dirty = _v.round(2) != res.price.round(2)
    end

    # remaining opts
    options.each do |k, v|
      if res.respond_to?(k)
        unless res.public_send(k) == v
          dirty = true
          res.public_send("#{ k }=", v)
        end
      end
    end

    if dirty
      res.save!
      MongoMapper::Document.redis.setex(cache_key, get_ttl,
                                 Octo::Utils.serialize(res))
    end
  else
    _args = args.merge(options)
    res = self.new(_args).save!
    MongoMapper::Document.redis.setex(cache_key, get_ttl,
                               Octo::Utils.serialize(res))
  end
  res
end
get_cached(args) click to toggle source

Perform a cache backed get @param [Hash] args The arguments hash for the record

to be found

@return [Cequel::Record::RecordSet] The record matching

# File lib/octocore-mongo/models.rb, line 162
def get_cached(args)
  cache_key = gen_cache_key(args)

  begin
    cached_val = MongoMapper::Document.redis.get(cache_key)
  rescue Exception
    cached_val = nil
  end

  unless cached_val
    res = where(args)
    result_count = res.count
    if result_count == 0
      return nil
    elsif result_count == 1
      cached_val = Octo::Utils.serialize(res.first)
      MongoMapper::Document.redis.setex(cache_key, get_ttl, cached_val)
    elsif result_count > 1
      cached_val = Octo::Utils.serialize(res)
      MongoMapper::Document.redis.setex(cache_key, get_ttl, cached_val)
    end
  end
  begin
    Octo::Utils.deserialize(cached_val)
  rescue Exception => e
    Octo.logger.error e
    nil
  end
end
recreate_from(obj) click to toggle source

Recreates this object from other object

# File lib/octocore-mongo/models.rb, line 75
def recreate_from(obj)
  keys = self.key_column_names
  args = {}
  if obj.respond_to?(:enterprise_id) and obj.respond_to?(:uid)
    args[keys.delete(:enterprise_id)] = obj.enterprise_id
    if keys.length == 1
      args[keys.first] = obj.uid
      self.get_cached(args)
    else
      puts keys.to_a.to_s
      raise NotImplementedError, 'See octocore/models.rb'
    end
  end
end

Private Instance Methods

gen_cache_key(args) click to toggle source

Generate cache key @param [Hash] args The arguments for fetching hash @return [String] Cache key generated

# File lib/octocore-mongo/models.rb, line 197
def gen_cache_key(args)
  args = args.flatten
  args.unshift(self.name.to_s)
  args.join('::')
end
get_ttl() click to toggle source
# File lib/octocore-mongo/models.rb, line 203
def get_ttl
  # default ttl of 1 hour
  ttl = 60
  if self.constants.include?(:TTL)
    ttl = self.const_get(:TTL)
  end

  # convert ttl into seconds
  ttl *= 60
end