Class: Mochigome::AggregationSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/model_extensions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ AggregationSettings

Returns a new instance of AggregationSettings.



261
262
263
264
265
# File 'lib/model_extensions.rb', line 261

def initialize(model)
  @model = model
  @options = {}
  @options[:fields] = []
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



259
260
261
# File 'lib/model_extensions.rb', line 259

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



258
259
260
# File 'lib/model_extensions.rb', line 258

def options
  @options
end

Instance Method Details

#fields(aggs) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/model_extensions.rb', line 267

def fields(aggs)
  unless aggs.is_a?(Enumerable)
    raise ModelSetupError.new "Call a.fields with an Enumerable"
  end

  @options[:fields].concat(aggs.map {|f|
    case f
    when String, Symbol then
      {
        :name => "%s %s" % [@model.name.pluralize, f.to_s.sub("_", " ")]
      }.merge(Mochigome::split_out_aggregation_procs(f))
    when Hash then
      if f.size == 1
        {
          :name => f.keys.first.to_s
        }.merge(Mochigome::split_out_aggregation_procs(f.values.first))
      else
        {
          :name => f[:name],
          :value_proc => Mochigome::value_proc(f[:value]),
          :agg_proc => Mochigome::aggregation_proc(f[:aggregation])
        }
      end
    else
      raise ModelSetupError.new "Invalid aggregation: #{f.inspect}"
    end
  })
end

#fields_in_ruby(aggs) ⇒ Object



305
306
307
308
309
310
311
312
313
314
# File 'lib/model_extensions.rb', line 305

def fields_in_ruby(aggs)
  @options[:fields].concat(aggs.map {|f|
    raise ModelSetupError.new "Invalid ruby agg #{f.inspect}" unless f.is_a?(Hash)
    {
      :name => f.keys.first.to_s,
      :ruby_proc => f.values.first,
      :in_ruby => true
    }
  })
end

#hidden_fields(aggs) ⇒ Object



296
297
298
299
300
301
302
303
# File 'lib/model_extensions.rb', line 296

def hidden_fields(aggs)
  orig_keys = Set.new @options[:fields].map{|a| a[:name]}
  fields(aggs)
  @options[:fields].each do |h|
    next if orig_keys.include? h[:name]
    h[:hidden] = true
  end
end