Class: Mochigome::AggregationSettings
- Inherits:
-
Object
- Object
- Mochigome::AggregationSettings
- Defined in:
- lib/model_extensions.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #fields(aggs) ⇒ Object
- #fields_in_ruby(aggs) ⇒ Object
- #hidden_fields(aggs) ⇒ Object
-
#initialize(model) ⇒ AggregationSettings
constructor
A new instance of AggregationSettings.
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
#model ⇒ Object (readonly)
Returns the value of attribute model.
259 260 261 |
# File 'lib/model_extensions.rb', line 259 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
258 259 260 |
# File 'lib/model_extensions.rb', line 258 def @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 |