Created
August 21, 2013 20:23
-
-
Save michiels/6299727 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DashboardMetrics < MetricsDefinition | |
metric "github stargazers" do |m| | |
m.reason = "Hallo!" | |
m.fetch_with do | |
5 | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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