Class: Octo::Segment

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document, Record
Defined in:
lib/octocore-mongo/models/enterprise/segment.rb

Overview

The segment class. Responsible for segments

Constant Summary

Constants included from MongoMapper::Document

MongoMapper::Document::DUMP_ATTRS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Record

#unique_id

Methods included from MongoMapper::Document

#marshal_dump, #marshal_load, redis, update_cache_config

Class Method Details

.find_by_enterprise_and_name(enterprise, name) ⇒ Object



58
59
60
# File 'lib/octocore-mongo/models/enterprise/segment.rb', line 58

def self.find_by_enterprise_and_name(enterprise, name)
  where({enterprise_id: enterprise._id, name_slug: name.to_slug})
end

Instance Method Details

#activateObject



37
38
39
# File 'lib/octocore-mongo/models/enterprise/segment.rb', line 37

def activate
  self.active = true
end

#create_name_slugObject

Creates name slug



33
34
35
# File 'lib/octocore-mongo/models/enterprise/segment.rb', line 33

def create_name_slug
  self.name_slug = self.name.to_slug
end

#data(ts = Time.now.floor) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/octocore-mongo/models/enterprise/segment.rb', line 41

def data(ts = Time.now.floor)
  args = {
    enterprise_id: self.enterprise._id,
    segment_slug: self.name_slug,
    ts: ts
  }
  res = Octo::SegmentData.where(args)
  if res.count > 0
    res.first
  elsif self.enterprise.fakedata?
    # populate a poser data
    val = [rand(1000..10000), rand(0.0..70.0)]
    args.merge!({ value: val })
    Octo::SegmentData.new(args).save!
  end
end