Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Commit

Permalink
Added support for cross-table attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
iasoon committed Feb 8, 2015
1 parent 7c2c38e commit 43ea380
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions lib/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def initialize model, &block
end

def fetch_stats stats, &group
hash = @model.all.group_by(&wrap_proc(&group))
hash = query.group_by(&wrap_proc(&group))
hash.default = get_stats stats, []
# compute stats
hash.each do |key, records|
Expand All @@ -122,14 +122,19 @@ def fetch_stats stats, &group

private

def attribute name, target
@attrmap[name] = target
def attribute name, target=name, through: nil
@attrmap[name] = Attribute.new name, target, through: through
end

def stat name, &block
@statmap[name] = Proc.new(&block)
end

def query
links = @attrmap.values.map(&:link).compact
@model.includes(links)
end

def wrap_proc &block
RecordWrapper.new(self).wrap_proc(&block)
end
Expand All @@ -142,18 +147,34 @@ def get_stats stats, records

end

class Attribute
attr_reader :name, :link

def initialize name, target, through: nil
@name = name
@target = target
@link = through
end

def get record
record = record.send(@link) if @link
record.send(@target)
end
end

class RecordWrapper

def initialize provider
# delegate methods
provider.model.attribute_names.each do |name|
define_singleton_method name do
@object.send(name)
end
end
# define the attrmap methods
provider.attrmap.each do |name, target|
# define attrmap methods
provider.attrmap.each do |name, attrib|
define_singleton_method(name) do
@object.send(target)
attrib.get @object
end
end
end
Expand All @@ -173,11 +194,23 @@ def self.builder &block
def self.test
Stats::builder do
target 'coder'
target 'repository'
target 'month' do
group_by { |a| a.date.try(&:month) }
end

provider Commit do
stat 'commits' do |cs| cs.count end
stat 'additions' do |cs| cs.map(&:additions).sum end
stat :deletions do |cs| cs.map(&:deletions).sum end
stat 'deletions' do |cs| cs.map(&:deletions).sum end
end

provider Bounty do
attribute :date, :claimed_at
attribute :coder_id, :claimant_id
attribute :repository_id, through: :issue

stat 'claimed' do |bs| bs.map(&:claimed_value).sum end
end
end
end
Expand Down

0 comments on commit 43ea380

Please sign in to comment.