Module: Timemaster::Document::ClassMethods

Defined in:
lib/timemaster/document.rb

Instance Method Summary collapse

Instance Method Details

#bucket_nameObject



22
23
24
# File 'lib/timemaster/document.rb', line 22

def bucket_name
  @bucket_name ||= self.to_s.tableize
end

#epoch(period = :hour, time = Time.now.utc, tag = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/timemaster/document.rb', line 30

def epoch(period = :hour, time = Time.now.utc, tag = nil)
  # res_instance = Resolution.new(options[:resolution], options[:time])
  index = Resolution.index(resolution_period)
  res_index = Resolution.index(period)
  resolutions = []
  RESOLUTIONS[0..index].each_with_index do |res_period, i| 
    resolutions[i] = Resolution.new(res_period, (i <= res_index) ? time : nil)
  end
  root = resolutions.shift.riak_object
  root.walk(resolutions.map(&:walk_spec).insert(-1, Riak::WalkSpec.new('requests', tag, true))).flatten
end

#get(id) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/timemaster/document.rb', line 14

def get(id)
  begin
    new bucket.get(id)
  rescue Riak::FailedRequest
    nil
  end
end

#period(resolution = :hour, start = 1.day.ago, stop = Time.now) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/timemaster/document.rb', line 42

def period(resolution = :hour, start = 1.day.ago, stop = Time.now)
  res_instance = Resolution.new(:hour)
  keys = (start..stop).send(:"to_#{res_instance.bucket_name}")
  keys.map! do |time|
    res_instance.time = time
    res_instance.key
  end
  keys
end

#resolution(period) ⇒ Object



26
27
28
# File 'lib/timemaster/document.rb', line 26

def resolution(period)
  self.resolution_period = period
end

#tag(new_tag, options = {}) ⇒ Object



52
53
54
55
# File 'lib/timemaster/document.rb', line 52

def tag(new_tag, options = {})
  self.tags ||= []
  self.tags << new_tag
end