Module: Timemaster::Document
- Defined in:
- lib/timemaster/document.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/timemaster/document.rb', line 107
def method_missing(method_symbol, *args)
method_name = method_symbol.to_s
if %w(? =).include?(method_name[-1,1])
method = method_name[0..-2]
operator = method_name[-1,1]
if operator == '='
set_value(method, args.first)
elsif operator == '?'
!@attributes[method].blank?
end
else
@attributes[method_name]
end
end
|
Class Method Details
.included(klass) ⇒ Object
3
4
5
6
7
8
9
10
11
|
# File 'lib/timemaster/document.rb', line 3
def self.included(klass)
klass.extend ClassMethods
klass.extend Helpers
klass.send(:include, Helpers)
klass.send(:attr_accessor, *[:key, :attributes])
klass.send(:attr_reader, :time)
klass.cattr_accessor :resolution_period
klass.cattr_accessor :tags
end
|
Instance Method Details
#[](key) ⇒ Object
99
100
101
|
# File 'lib/timemaster/document.rb', line 99
def [](key)
@attributes[key]
end
|
#[]=(key, value) ⇒ Object
103
104
105
|
# File 'lib/timemaster/document.rb', line 103
def []=(key, value)
@attributes[key] = value
end
|
#bucket_name ⇒ Object
130
131
132
|
# File 'lib/timemaster/document.rb', line 130
def bucket_name
@bucket_name ||= self.class.bucket_name
end
|
#initialize(params = {}) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/timemaster/document.rb', line 58
def initialize(params = {})
case params
when Hash
@time = params.delete(:time) || Time.now.utc
@attributes = Hashie::Mash.new(params)
when Riak::RObject
self.riak_object = params
end
end
|
#make_links ⇒ Object
68
69
70
71
|
# File 'lib/timemaster/document.rb', line 68
def make_links
return link_for if self.class.tags.blank?
self.class.tags.map{|key| link_for("#{key}_#{@attributes[key.to_sym]}")}.flatten.compact
end
|
#resolution_docs ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/timemaster/document.rb', line 134
def resolution_docs
RESOLUTIONS[0..resolution_index].each do |period|
res_period = resolutions[period]
unless res_period.exists?
if (child = resolutions[res_period.next])
res_period.links << child.link
end
res_period.store
if (parent = resolutions[res_period.prev])
parent.update_links!(res_period.link)
end
end
end
end
|
#resolution_index ⇒ Object
84
85
86
|
# File 'lib/timemaster/document.rb', line 84
def resolution_index
@resolution_index ||= RESOLUTIONS.index(self.class.resolution_period)
end
|
#resolutions ⇒ Object
77
78
79
80
81
82
|
# File 'lib/timemaster/document.rb', line 77
def resolutions
@resolutions ||= RESOLUTIONS[0..resolution_index].inject({}) do |hash, period|
hash[period] = Resolution.new(period, time)
hash
end
end
|
#save ⇒ Object
149
150
151
152
153
154
155
156
|
# File 'lib/timemaster/document.rb', line 149
def save
resolution_docs
data = @attributes
store
self.key = riak_object.key
linked_res = resolutions[self.class.resolution_period]
linked_res.update_links!(make_links)
end
|
#set_value(method, val) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/timemaster/document.rb', line 122
def set_value(method, val)
if val.blank?
@attributes.delete(method)
else
@attributes[method] = val
end
end
|
#time=(timestamp) ⇒ Object
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/timemaster/document.rb', line 88
def time=(timestamp)
case timestamp
when Date
self.time = timestamp.to_time
when Time
@time = timestamp.utc
when Riak::Link
self.time = time_from_link(timestamp)
end
end
|
#time_from_link(link) ⇒ Object
73
74
75
|
# File 'lib/timemaster/document.rb', line 73
def time_from_link(link)
Time.utc(*link.url.split('/').last.split('_'))
end
|