Skip to content

Instantly share code, notes, and snippets.

@jrdi

jrdi/patient.rb Secret

Created October 15, 2013 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jrdi/b3f824fa4e7531c43bfd to your computer and use it in GitHub Desktop.
Save jrdi/b3f824fa4e7531c43bfd to your computer and use it in GitHub Desktop.
class Patient
include Mongoid::Document
field :created_at, type: Time
field :updated_at, type: Time
def self.group_by(field, format = 'day')
reduce = %Q{
function(key, values) {
var result = { count: 0 };
values.forEach(function(value) {
result.count += value.count;
});
return result;
}
}
Patient.map_reduce(Patient.map(field, format), reduce).out(inline: true)
end
def self.map(date_field, format = 'day')
date_format = case format
when 'month'
"mm + '/' + yyyy"
when 'year'
"yyyy"
else
"dd + '/' + mm + '/' + yyyy"
end
%Q{
function() {
var dateTime = new Date(this.#{date_field});
var dd = dateTime.getDate();
var mm = dateTime.getMonth() + 1;
var yyyy = dateTime.getFullYear();
var date = #{date_format};
emit(date, { count: 1 });
}
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment