slims down the result base class

pull/6827/head
jwollert 14 years ago
parent 4512e2739f
commit d2fa0ab90c
  1. 102
      app/models/cost_query/result.rb

@ -1,110 +1,12 @@
module CostQuery::Result
class Base
attr_accessor :parent, :type, :important_fields
attr_accessor :key
attr_reader :value
alias values value
include Enumerable
include CostQuery::QueryUtils
def initialize(value)
@important_fields ||= []
@type = :direct
@value = value
end
def recursive_each_with_level(level = 0, depth_first = true, &block)
block.call(level, self)
end
def recursive_each
recursive_each_with_level { |level, result| yield result }
end
def to_hash
fields.dup
end
def [](key)
fields[key]
end
def grouped_by(fields, type, important_fields = [])
@grouped_by ||= {}
list = begin
@grouped_by[fields] ||= begin
# sub results, have fields
# i.e. grouping by foo, bar
data = group_by do |entry|
# index for group is a hash
# i.e. { :foo => 10, :bar => 20 } <= this is just the KEY!!!!
fields.inject({}) { |hash, key| hash.merge key => entry.fields[key] }
end
# map group back to array, all fields with same key get grouped into one list
data.keys.map { |f| CostQuery::Result.new data[f], f, type, important_fields }
end
end
# create a single result from that list
CostQuery::Result.new list, {}, type, important_fields
end
include Report::Result
class Base < Report::Result::Base
def inspect
"<##{self.class}: @fields=#{fields.inspect} @type=#{type.inspect} " \
"@size=#{size} @count=#{count} @units=#{units} @real_costs=#{real_costs}>"
end
def row?
type == :row
end
def column?
type == :column
end
def direct?
type == :direct
end
def each_row
end
def final?(type)
type? type and (direct? or first.type != type)
end
def type?(type)
self.type == type
end
def depth_of(type)
if type? type or (type == :column and direct?) then 1
else 0
end
end
def final_number(type)
return 1 if final? type
return 0 if direct?
@final_number ||= {}
@final_number[type] ||= sum { |v| v.final_number type }
end
def final_row?
final? :row
end
def final_column?
final? :column
end
def render(keys = important_fields)
fields.map { |k,v| yield(k,v) if keys.include? k }.join
end
def set_key(index = [])
self.key = index.map { |k| map_field(k, fields[k]) }
end
def display_costs?
display_costs > 0
end

Loading…
Cancel
Save