Class: CalculatingCollection
Instance Attribute Summary
#coll
Instance Method Summary
collapse
#method_missing, #user_coll
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class WrappingCollection
Instance Method Details
#add_column(name, blk) ⇒ Object
44
45
46
47
|
# File 'lib/formula_eval/calculating_collection.rb', line 44
def add_column(name,blk)
blk = FormulaEval.new(:formula => blk, :coll => coll) if blk.kind_of?(String)
self.column_hash[name.to_s] = blk
end
|
#add_constant_column(name, formula) ⇒ Object
40
41
42
43
|
# File 'lib/formula_eval/calculating_collection.rb', line 40
def add_constant_column(name,formula)
arr = eval(formula).to_a
constants_hash[name] = arr
end
|
#calc_enriched_doc(doc, ops) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/formula_eval/calculating_collection.rb', line 71
def calc_enriched_doc(doc,ops)
doc = cleaned_doc(doc)
column_hash.each_with_str_key do |col,blk|
if KeyParts.single?(col)
val = blk.call(doc)
doc.dot_set(col,val)
else
doc.dot_set(col) do |obj|
multi = MultiEval.new(:objs => [obj,doc])
blk.call(multi)
end
end
end
doc.to_unwrapped
end
|
#cleaned_doc(doc) ⇒ Object
48
49
50
51
52
53
|
# File 'lib/formula_eval/calculating_collection.rb', line 48
def cleaned_doc(doc)
column_hash.keys.each do |k|
doc.dot_set(k,nil)
end
doc
end
|
#constants_enriched_doc(doc, ops) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/formula_eval/calculating_collection.rb', line 54
def constants_enriched_doc(doc,ops)
constants_hash.each do |col,vals|
vals = vals.to_a
KeyParts.with_parts(col) do |start,lst,mult|
if mult
doc[start] ||= []
vals.each_with_index do |val,i|
field = "#{start}.#{i}.#{lst}"
doc.dot_set(field,val)
end
elsif ops[:row_index]
doc[col] = vals[ops[:row_index]]
end
end
end
doc
end
|
#enriched_doc(doc, other_ops = {}) ⇒ Object
86
87
88
89
|
# File 'lib/formula_eval/calculating_collection.rb', line 86
def enriched_doc(doc,other_ops={})
doc = constants_enriched_doc(doc,other_ops)
doc = calc_enriched_doc(doc,other_ops)
end
|
#find(selector = {}, ops = {}) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/formula_eval/calculating_collection.rb', line 127
def find(selector={},ops={})
res = coll.find(selector,ops)
if res.respond_to?(:each)
res.select { |doc| needs_calc?(doc) }.each { |doc| save(doc) }
end
res
end
|
90
91
92
93
|
# File 'lib/formula_eval/calculating_collection.rb', line 90
def keys
ks = column_hash.keys.map { |x| x.split('.').first }.uniq
coll.keys.make_last(ks)
end
|
#needs_calc?(doc) ⇒ Boolean
123
124
125
126
|
# File 'lib/formula_eval/calculating_collection.rb', line 123
def needs_calc?(doc)
doc_dt = doc['_admin'].andand['last_calc_dt'] || Time.local(1970,1,1)
user_coll_last_calc_dt > doc_dt
end
|
#save(doc, ops = {}, other_ops = {}) ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/formula_eval/calculating_collection.rb', line 94
def save(doc,ops={},other_ops={})
doc = enriched_doc(doc,other_ops)
doc.nested_hash_set '_admin','last_calc_dt',Time.now
res = coll.save(doc,ops)
doc[:_id] = res
doc
end
|
#update_calcs! ⇒ Object
102
103
|
# File 'lib/formula_eval/calculating_collection.rb', line 102
def update_calcs!
end
|
#update_calcs_real! ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/formula_eval/calculating_collection.rb', line 104
def update_calcs_real!
constants_max = constants_hash.values.map { |x| x.to_a.size }.max || 0
find({},{:limit => 9999}).each_with_index do |row,i|
save(row,{},:row_index => i)
end
if constants_max > count
(count...constants_max).each do |i|
save({},{},:row_index => i)
end
end
rescue => exp
mylog 'update_calcs', :trace => exp.backtrace.join("\n"), :message => exp.message
raise exp
end
|
#update_row(row_id, fields) ⇒ Object
118
119
120
121
122
|
# File 'lib/formula_eval/calculating_collection.rb', line 118
def update_row(row_id,fields)
row = coll.update_row(row_id,fields)
mylog 'calc_update_row', :row => row, :coll => coll.class
row = save(row)
end
|