Skip to content

Instantly share code, notes, and snippets.

@benvds
Last active December 10, 2015 15:18
Show Gist options
  • Save benvds/4452980 to your computer and use it in GitHub Desktop.
Save benvds/4452980 to your computer and use it in GitHub Desktop.
Sequel dataset extension. For a reporting page query params can be given. These are used for the selects, joins, filters etc. I tried an approach where methods (like: apply_joins, apply_filters, etc.) passed the dataset around but this is cumbersome. Thinking you can also extend dataset it would prevent passing the dataset around. But now im hav…
module Stats
def select_metrics(metrics)
metrics.inject(self) do |ds, metric|
ds.select_metric(metric)
end
end
def select_metric(metric)
case metric
when 'total'
select_append{ count(subject_picks__id).as(total) }
when 'odd_avg'
select_append{ round(avg(subject_picks__odd), ROUND_TO_DECIMALS).as(odd_avg) }
# ...
end
end
end
DB[:my_table].extend(Stats).
select(:user_id).
select_metrics(['total', 'odd_avg']).
group(:user_id).
all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment