Class: MongoidHashQuery::Aggregation
- Inherits:
-
Object
- Object
- MongoidHashQuery::Aggregation
- Includes:
- Helpers
- Defined in:
- lib/mongoid_hash_query/aggregation.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #apply ⇒ Object
- #apply_aggregations(available_aggr, asked_aggr, field) ⇒ Object
-
#initialize(resource, params, model: nil) ⇒ Aggregation
constructor
A new instance of Aggregation.
Methods included from Helpers
Constructor Details
#initialize(resource, params, model: nil) ⇒ Aggregation
Returns a new instance of Aggregation.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 7 def initialize(resource, params, model: nil) @configuration = Module.nesting.last.configuration @resource = resource @params = HashWithIndifferentAccess.new(params) @model = model unless @model @model = model_class_name(@resource) end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
5 6 7 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 5 def configuration @configuration end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 5 def model @model end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 5 def params @params end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 5 def resource @resource end |
Instance Method Details
#apply ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 18 def apply if params[:aggregate].is_a? Hash = HashWithIndifferentAccess.new params[:aggregate].each do |field, asked_aggrs| #next if @model.fields[field].nil? #allow on embedded documents too next unless params[:aggregate][field].is_a? Hash if @model.fields[field] case @model.fields[field].[:type].to_s.downcase.to_sym when :integer, :float, :bigdecimal [field] = apply_aggregations( {avg: :avg, sum: :sum, max: :max, min: :min}, params[:aggregate][field], field ) when :date, :datetime, :time, :timewithzone, [field] = apply_aggregations( {max: :max, min: :min}, params[:aggregate][field], field ) end else [field] = apply_aggregations( {avg: :avg, sum: :sum, max: :max, min: :min}, params[:aggregate][field], field ) end end end return {aggregations: } end |
#apply_aggregations(available_aggr, asked_aggr, field) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoid_hash_query/aggregation.rb', line 55 def apply_aggregations(available_aggr, asked_aggr, field) = HashWithIndifferentAccess.new available_aggr.each do |k, v| if asked_aggr[k] [k] = resource.send(v,field) [k] = [k].to_f if [k].is_a? BigDecimal end end return end |