Class: Timemaster::Resolution

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/timemaster/resolution.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#bucket, #exists?, #link_for, #path, #riak_object, #walk_spec

Constructor Details

#initialize(name, time) ⇒ Resolution

Returns a new instance of Resolution.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/timemaster/resolution.rb', line 11

def initialize(name, time)
  @name = name
  @time = case time
  when Time then
    time
  when String
    Time.utc(*time.split('_'))
  when Nil
    Time.now.utc
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/timemaster/resolution.rb', line 10

def name
  @name
end

#timeObject

Returns the value of attribute time.



10
11
12
# File 'lib/timemaster/resolution.rb', line 10

def time
  @time
end

Class Method Details

.index(resolution) ⇒ Object



6
7
8
# File 'lib/timemaster/resolution.rb', line 6

def self.index(resolution)
  RESOLUTIONS.index(resolution)
end

Instance Method Details

#bucket_nameObject



41
42
43
# File 'lib/timemaster/resolution.rb', line 41

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

#default_tagObject



27
28
29
30
31
# File 'lib/timemaster/resolution.rb', line 27

def default_tag
  return @tag if @tag
  return nil unless @time
  @tag = @time.to_s(name)
end

#indexObject



23
24
25
# File 'lib/timemaster/resolution.rb', line 23

def index
  @index ||= self.class.index(name)
end

#keyObject



51
52
53
54
55
56
# File 'lib/timemaster/resolution.rb', line 51

def key
  return @key if @key
  # join string up to resolution and user to format time
  return @key = nil if index.blank?
  @key = RESOLUTIONS[0..index].inject(''){|str, res| "#{str}_#{time.to_s(res)}"}[1..-1]
end


58
59
60
# File 'lib/timemaster/resolution.rb', line 58

def link
  @link ||= link_for(default_tag)
end

#nextObject



37
38
39
# File 'lib/timemaster/resolution.rb', line 37

def next
  RESOLUTIONS[index + 1]
end

#prevObject



33
34
35
# File 'lib/timemaster/resolution.rb', line 33

def prev
  (index >= 1) ? RESOLUTIONS[index - 1] : nil
end

#update_links!(*args) ⇒ Object



45
46
47
48
49
# File 'lib/timemaster/resolution.rb', line 45

def update_links!(*args)
  obj = bucket.get(key)
  obj.links.merge args.flatten
  obj.store
end