crunchr

Given an ORM-model that makes snapshots with counts off your data
When I include Crunchr
Then I can do all kinds of nifty calculations

Synopsis

# Given an ORM-mode that makes snapshots
class Statistic < ActiveRecord::Base
  serialize :data

  def self.snapshot
    Statistic.create(
      data: {
        keys: Key.count,
        doors: Door.count,
        rooms: {
          count: Room.count,
          occupied: Room.occupied.count
        }
      }
    )
  end

  # When I include Crunchr
  include Crunchr
end

# Then I can doo all kinds of nifty things
s = Statistic.last

# like, fetch data
s.fetch('keys')                # 10
s.fetch('doors')               # 8
s.fetch('rooms/count')         # 7

# do calculations
s.fetch('keys x doors')        # 80
s.fetch('keys - doors')        # 2 spare keys...
s.fetch('doors / rooms/count') # 1.1428 doors per room

# make deltas
delta = s.delta(Statistic.first)
delta.fetch('keys')           # 10 (now) - 5 (then) = 5
delta.fetch('keys - doors')   # 5 (see above) - 8   = -3
delta.fetch('rooms/occupied') # 0 (did not change)

# make tables
rows = Statistic.where( "created_at > ?", 1.week.ago )
Statistic.as_table( rows, keys: ['keys',  'doors', 'keys / doors'] )
# => [
#   [ 9, 8, 1.125 ]
#   [ 9, 8, 1.125 ]
#   [ 7, 8, 0.875 ]
#   [ 10, 8, 1.25 ]
# ]

TODO

Contributing to crunchr

Copyright © 2013 Hartog C. de Mik. See LICENSE.txt for further details.