Skip to content

Instantly share code, notes, and snippets.

@michiels
Created August 21, 2013 20:23
Show Gist options
  • Save michiels/6299727 to your computer and use it in GitHub Desktop.
Save michiels/6299727 to your computer and use it in GitHub Desktop.
class DashboardMetrics < MetricsDefinition
metric "github stargazers" do |m|
m.reason = "Hallo!"
m.fetch_with do
5
end
end
end
class Metric < ActiveRecord::Base
attr_accessor :reason
has_many :daily_metrics
def fetch_with(block)
@block = block
end
def fetch
daily_metric = daily_metrics.where(date: Date.today).first_or_initialize
value = @block.call
daily_metric.value = value
daily_metric.save
end
end
class MetricsDefinition
cattr_accessor :definition
attr_accessor :metrics
class << self
def inherited(base)
super
MetricsDefinition.definition ||= self.new
end
def metric(name)
m = Metric.new
yield(m)
MetricsDefinition.definition.metrics << m
end
end
def initialize
@metrics = []
end
def run_fetchers()
@metrics.each do |m|
m.fetch
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment