Class: TTCalc
- Inherits:
-
Object
- Object
- TTCalc
- Defined in:
- lib/ttcalc.rb
Instance Method Summary collapse
- #count(countfield) ⇒ Object
-
#initialize(input) ⇒ TTCalc
constructor
A new instance of TTCalc.
- #summatching(attsum, attmatch) ⇒ Object
Constructor Details
#initialize(input) ⇒ TTCalc
Returns a new instance of TTCalc.
4 5 6 |
# File 'lib/ttcalc.rb', line 4 def initialize(input) @input = JSON.parse(input) end |
Instance Method Details
#count(countfield) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ttcalc.rb', line 32 def count(countfield) valhash = Hash.new @input.each do |i| if i[countfield].is_a? Array i[countfield].each do |j| if valhash[j] valhash[j] = valhash[j] + 1 else valhash[j] = 1 end end else if valhash[i[countfield]] valhash[i[countfield]] += 1 else valhash[i[countfield]] = 1 end end end outarray = Array.new valhash.each do |k,v| temphash = Hash.new temphash[countfield] = k temphash["count"] = v outarray.push(temphash) end return JSON.pretty_generate(outarray) end |
#summatching(attsum, attmatch) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ttcalc.rb', line 8 def summatching(attsum, attmatch) @attsum = attsum @attmatch = attmatch attvals = Hash.new @input.each do |i| if attvals[i[@attmatch]] attvals[i[@attmatch]] = attvals[i[@attmatch]]+i[@attsum] else attvals[i[@attmatch]] = i[@attsum] end end outarray = Array.new attvals.each do |k,v| temphash = Hash.new temphash[@attmatch] = k temphash[@attsum] = v outarray.push(temphash) end return JSON.pretty_generate(outarray) end |